2023年7月26日发(作者:)
selenium⽰例_SeleniumfindElement和findElements⽰例selenium ⽰例Whenever you want to interact with a web page, we require a user to locate the web elements. We usually start by findingthe HTML elements on the page whenever we plan to automate any web application using WebDriver.每当您想与⽹页进⾏交互时,我们都需要⽤户找到⽹页元素。 每当我们计划使⽤WebDriver⾃动化任何Web应⽤程序时,我们通常⾸先在页⾯上找到HTML元素。defines two methods for identifying the elements, they are findElement and findElements.定义了两种⽤于标识元素的⽅法,它们是findElement和findElements 。1. findElement: This command is used to uniquely identify a web element within the web ement :此命令⽤于唯⼀标识⽹页中的⽹页元素。2. findElements: This command is used to uniquely identify the list of web elements within the web ements :此命令⽤于唯⼀标识⽹页中的Web元素列表。There are multiple ways to uniquely identify a web element within the web page such as ID, Name, Class Name, LinkText,PartialLinkText, TagName, and XPath.有多种⽅法可以唯⼀地标识⽹页中的Web元素,例如ID,名称,类名,LinkText,PartialLinkText,TagName和XPath。findElement和findElements⽅法之间的区别 (Difference between findElement and findElementsMethods)FindElement() Method:FindElement()⽅法 :This command is used to access any single element on the web page此命令⽤于访问⽹页上的任何单个元素It will return the object of the first matching element of the specified locator它将返回指定定位符的第⼀个匹配元素的对象It will throw
NoSuchElementException when it fails to identify the element⽆法识别元素时将抛出NoSuchElementExceptionFindElements() Method:FindElements()⽅法 :This command is used to uniquely identify the list of web elements within the web page.此命令⽤于唯⼀标识⽹页中的Web元素列表。The usage of this method is very limited这种⽅法的⽤途⾮常有限If the element doesn’t exist on the page then, then it will return value with an empty list如果该元素在页⾯上不存在,则它将返回带有空列表的值Selenium findElement命令 (Selenium findElement Command)Find Element command takes in the By object as a parameter and returns an object of type WebElement. By object can beused with various locator strategies such as ID, Name, ClassName, link text, XPath, etc.“查找元素”命令将“按”对象作为参数,并返回WebElement类型的对象。 按对象可以与各种定位器策略⼀起使⽤,例如ID,Name,ClassName,链接⽂本,XPath等。FindElement命令的语法 (Syntax of FindElement command)WebElement elementName = ement(rStrategy("LocatorValue"));Locator Strategy can be any of the following values.定位器策略可以是以下任意值。IDIDName名称Class Name班级名称Tag Name标签名Link Text连结⽂字Partial Link Text部分链接⽂字XPathXPathLocator Value is the unique value using which we can identify the web element. It is the core responsibility of developers andtesters to make ensure that web elements are uniquely identified by using certain properties such as ID or Name.定位器值是可以⽤来识别⽹络元素的唯⼀值。 开发⼈员和测试⼈员的核⼼责任是确保使⽤某些属性(例如ID或Name)来唯⼀标识Web元素。Example:例:WebElement login= ement(xt("Login"));Selenium findElements命令 (Selenium findElements Command)Selenium findElements command takes in By object as the parameter and returns a list of web elements. It returns an emptylist if no elements found using the given locator strategy and locator um findElements命令将By对象作为参数,并返回Web元素列表。 如果没有使⽤给定的定位器策略和定位器值找到任何元素,它将返回⼀个空列表。FindElements命令的语法 (Syntax of FindElements command)List
import ver;
import Driver;public class SeleniumFindElement {
public static void main (String [] args){
perty("","D:"); WebDriver driver= new ChromeDriver(); ().ze(): (:"/nget/user-registration");//Find the radio button for "Male" by using ID and click on ement(("M")).click(); }
}如何使⽤Selenium findElements命令 (How to use Selenium findElements Command)The following application is used for demo purpose以下应⽤程序⽤于演⽰⽬的Scenario情境1. Open the /nget/user-registration for AUT打开AUT的/nget/user-registration2. Find the text of radio buttons and print on console查找单选按钮的⽂本并在控制台上打印package ements;import ;import ;import ver;import ment;import Driver;public class SeleniumFindElements { public static void main(String[] args) { perty("","D:"); WebDriver driver= new ChromeDriver(); ("/nget/user-registration"); List
JournalDev-2 Java example code to find element matching link or partial link text:Java⽰例代码,⽤于查找与链接或部分链接⽂本匹配的元素 :WebElement link = ement(xt("JournalDev-1"));WebElement link = ement(lLinkText("JournalDev-2"));5.通过CssSelector (5. By CssSelector)Command: ement(ector(
Java example code to find element matching link or partial link text:Java⽰例代码,⽤于查找与链接或部分链接⽂本匹配的元素 :WebElement emailText = ement(ector("input#email"));6.通过XPath (6. By XPath)Command: ement((
发布者:admin,转转请注明出处:http://www.yc00.com/web/1690366146a338885.html
评论列表(0条)