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 finding
the 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 page.
findElement :此命令⽤于唯⼀标识⽹页中的⽹页元素。
2. findElements: This command is used to uniquely identify the list of web elements within the web page.
findElements :此命令⽤于唯⼀标识⽹页中的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 findElements Methods)
FindElement() Method:
FindElement()⽅法 :
This command is used to access any single element on the web page
find查命令的使用此命令⽤于访问⽹页上的任何单个元素
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
⽆法识别元素时将抛出NoSuchElementException
FindElements() 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 be used 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 = driver.findElement(By.LocatorStrategy("LocatorValue"));
Locator Strategy can be any of the following values.
定位器策略可以是以下任意值。
ID
ID
Name
名称
Class Name
班级名称
Tag Name
标签名
Link Text
连结⽂字
Partial Link Text
部分链接⽂字
XPath
XPath
Locator Value is the unique value using which we can identify the web element. It is the core responsibility of developers and testers to make ensure that web elements are uniquely identified by using certain properties such as ID or Name.
定位器值是可以⽤来识别⽹络元素的唯⼀值。 开发⼈员和测试⼈员的核⼼责任是确保使⽤某些属性(例如ID或Name)来唯⼀标识Web元素。
Example:
例:
WebElement login= driver.findElement(By.linkText("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 empty list if no elements found using the given locator strategy and locator value.
Selenium findElements命令将By对象作为参数,并返回Web元素列表。 如果没有使⽤给定的定位器策略和定位器值到任何元素,它将返回⼀个空列表。
FindElements命令的语法 (Syntax of FindElements command)
List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
Example:
例:
List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));
如何使⽤Selenium findElement命令 (How to use Selenium findElement Command) The following application is used for demo purpose:
以下应⽤程序⽤于演⽰⽬的:
Scenario
情境
1. Open the in/nget/user-registration for AUT
打开AUT的in/nget/user-registration
2. Find and click radio button
查并单击单选按钮
package com.journaldev.selenium.findelement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumFindElement {
public static void main (String [] args){
System.setProperty("webdriver.chrome.driver","D:\\Drivers\\");
WebDriver driver= new ChromeDriver();
driver.manage().window.maximize():
<(:"in/nget/user-registration");
//Find the radio button for "Male" by using ID and click on it
driver.findElement(By.id("M")).click();
}
}
如何使⽤Selenium findElements命令 (How to use Selenium findElements Command) The following application is used for demo purpose
以下应⽤程序⽤于演⽰⽬的
Scenario
情境
1. Open the in/nget/user-registration for AUT
打开AUT的in/nget/user-registration
2. Find the text of radio buttons and print on console
查单选按钮的⽂本并在控制台上打印
package com.journaldev.selenium.findelements;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumFindElements {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Drivers\\");
WebDriver driver= new ChromeDriver();
<("in/nget/user-registration");
List<WebElement> elements = driver.findElements(By.id("M"));
System.out.println("Number of elements:" +elements.size());
for(int i=0; i<elements.size(); i++){
System.out.println("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 locator object known as <"By">. There are various kinds of “By” strategies which you can use depending on your requirement.
Selenium Webdriver通过使⽤findElement(By.)⽅法引⽤Web元素。 findElement⽅法使⽤称为<"By">的定位器对象。 您可以根据需要使⽤多种“按”策略。
1.按编号 (1. By ID)
Command: driver.findElement(By.id(<element ID>))
命令 :driver.findElement(By.id(<;元素ID>))
Example: <input id=”JournalDev”>
⽰例 :<input id =” JournalDev”>
Java example code to find the input element by id
Java⽰例代码,通过ID查输⼊元素
WebElement user = driver.findElement(By.id("JournalDev"));
2.按名称 (2. By Name)
Command: driver.findElement(By.name(<element-name>))
命令 :driver.findElement(By.name(<element-name>))
Example: <input name=”JournalDev”>
⽰例 :<;输⼊名称=“ JournalDev”>
Java example code to find the input element by name
Java⽰例代码按名称查输⼊元素
WebElement user = driver.findElement(By.name("JournalDev"));
3.按班级名称 (3. By Class Name)
Command: driver.findElement(By.className(<element-class>))
命令 :driver.findElement(By.className(<element-class>))
Example: <input class=”JournalDev”>
⽰例 :<input class =” JournalDev”>
Java example code to find the input element by className.
通过className查输⼊元素的Java⽰例代码。
WebElement user = driver.findElement(By.className("JournalDev"));
4.通过LinkText (4. By LinkText)
Command: driver.findElement(By.linkText(<link text>))
命令 :driver.findElement(By.linkText(<link text>))
Example:
<a href=”#test1″>JournalDev-1</a>
<a href=”#test2″>JournalDev-2</a>
范例 :
<a href=”#test1”> JournalDev-1 </a>
<a href=”#test2”> JournalDev-2 </a>
Java example code to find element matching link or partial link text:
Java⽰例代码,⽤于查与链接或部分链接⽂本匹配的元素 :
WebElement link = driver.findElement(By.linkText("JournalDev-1"));
WebElement link = driver.findElement(By.partialLinkText("JournalDev-2"));
5.通过CssSelector (5. By CssSelector)
Command: driver.findElement(By.cssSelector(<css-selector>))
命令:driver.findElement(By.cssSelector(<css-selector>))
Example:
<input class=”email” id=”email” type=”text” placeholder=”xxx@email”> <input class=”btn btn-small” type=”submit” value=”Subscribe to blog>
范例 :
<input class =“ email” id =“电⼦邮件” type =“⽂本”占位符=“ xxx@email”> <input class =“ btn btn-small” type =“提交” value =“订阅博客>
Java example code to find element matching link or partial link text:
Java⽰例代码,⽤于查与链接或部分链接⽂本匹配的元素 :
WebElement emailText = driver.findElement(By.cssSelector("input#email"));