selenium示例_SeleniumfindElement和findElements示例

selenium示例_SeleniumfindElement和findElements示例

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 elementName = ements(rStrategy("LocatorValue"));Example:例:List listOfElements = ements(("//div"));如何使⽤Selenium findElement命令 (How to use Selenium findElement Command)The following application is used for demo purpose:以下应⽤程序⽤于演⽰⽬的:Scenario情境1. Open the /nget/user-registration for AUT打开AUT的/nget/user-registration2. Find and click radio button查找并单击单选按钮package ement;import ;

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 elements = ements(("M")); n("Number of elements:" +()); for(int i=0; i<(); i++){ n("Radio button text:" + (i).getAttribute("value")); } }}多种策略访问Selenium定位器 (Multiple By Strategies To Access Selenium Locators)Selenium Webdriver references the web elements by using findElement(By.) method. The findElement method uses a locatorobject known as <"By">. There are various kinds of “By” strategies which you can use depending on your um Webdriver通过使⽤findElement(By.)⽅法引⽤Web元素。 findElement⽅法使⽤称为<"By">的定位器对象。 您可以根据需要使⽤多种“按”策略。1.按编号 (1. By ID)Command: ement(())命令 :ement((<元素ID>))Example: ⽰例 :Java example code to find the input element by idJava⽰例代码,通过ID查找输⼊元素WebElement user = ement(("JournalDev"));2.按名称 (2. By Name)Command: ement(())命令 :ement(())Example: ⽰例 :<输⼊名称=“ JournalDev”>Java example code to find the input element by nameJava⽰例代码按名称查找输⼊元素WebElement user = ement(("JournalDev"));3.按班级名称 (3. By Class Name)Command: ement(ame())命令 :ement(ame())Example: ⽰例 :Java example code to find the input element by className.通过className查找输⼊元素的Java⽰例代码。WebElement user = ement(ame("JournalDev"));4.通过LinkText (4. By LinkText)Command: ement(xt())命令 :ement(xt())Example:JournalDev-1JournalDev-2范例 :

JournalDev-1

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())命令:ement(ector())Example:范例 :

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(())命令 :ement(())Java example code for XPath:XPath的Java⽰例代码 :// Absolute pathWebElement item = ement(("html/head/body/table/tr/td"));// Relative pathWebElement item = ement(("//input"));// Finding elements using indexesWebElement item = ement(("//input[2]"));selenium ⽰例

发布者:admin,转转请注明出处:http://www.yc00.com/web/1690366146a338885.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信