每当你想与网页互动时,我们要求用户找到网页元素. 通常,我们每当我们计划使用 WebDriver 来自动化任何网页应用程序时,就开始寻找网页元素。
- findElement:此命令用于在网页中单独识别网页元素 。
您可以通过多种方式在网页中单独识别网页元素,例如 ID、Name、Class Name、LinkText、PartialLinkText、TagName 和 XPath。
findElement 和 findElements 方法的区别
方法() 方法**:
- 此命令用于访问网页上的任何单个元素
- 它会返回指定的定位器 的第一个匹配元素的对象* 当它无法识别元素 时,它会扔 NoSuchElementException
方法() 方法**:
- 此命令用于在网页中单独识别网页元素列表
- 这种方法的使用非常有限
- 如果该元素在网页上不存在,那么它将以空列表 返回值。
Selenium Find 元素指令
Find Element 命令将 By 对象作为参数,并返回 WebElement 类型的对象. By 对象可用于各种定位策略,如 ID、Name、ClassName、链接文本、XPath 等。
FindElement 命令的语法
1WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));
Locator 策略可以是下列值中的任何一个。
- ID
- 名称
- 类名称
- 标签名称
- 链接文本
- 部分链接文本
- XPath
Locator Value 是我们可以识别 Web 元素的唯一值,开发人员和测试人员的核心责任是通过使用某些属性(例如 ID 或 Name)确保 Web 元素的唯一识别。
1WebElement login= driver.findElement(By.linkText("Login"));
Selenium findElements 指令
Selenium findElements 命令将 By object 作为参数,并返回 Web 元素列表,如果没有使用给定的定位器策略和定位器值找到元素,则返回空列表。
FindElements 命令的语法
1List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
** 例子:**
1List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));
如何使用 Selenium findElement 命令
以下应用程序用于演示目的: https://www.irctc.co.in/nget/user-registration
** 场景**
- 打开 https://www.irctc.co.in/nget/user-registration for AUT
- 查找并点击无线电按钮
1package com.journaldev.selenium.findelement;
2
3import org.openqa.selenium.By;
4import org.openqa.selenium.WebDriver;
5import org.openqa.selenium.chrome.ChromeDriver;
6
7public class SeleniumFindElement {
8 public static void main (String [] args){
9
10 System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
11 WebDriver driver= new ChromeDriver();
12 driver.manage().window.maximize():
13 driver.get(:"https://www.irctc.co.in/nget/user-registration");
14
15//Find the radio button for "Male" by using ID and click on it
16driver.findElement(By.id("M")).click();
17
18 }
19}
如何使用 Selenium findElements 命令
下面的应用程序用于演示目的 https://www.irctc.co.in/nget/user-registration
** 场景**
- 打开 https://www.irctc.co.in/nget/user-registration for AUT
- 查找无线电按钮的文本,并在控制台 打印
1package com.journaldev.selenium.findelements;
2
3import java.util.List;
4
5import org.openqa.selenium.By;
6import org.openqa.selenium.WebDriver;
7import org.openqa.selenium.WebElement;
8import org.openqa.selenium.chrome.ChromeDriver;
9
10public class SeleniumFindElements {
11
12 public static void main(String[] args) {
13
14 System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
15 WebDriver driver= new ChromeDriver();
16 driver.get("https://www.irctc.co.in/nget/user-registration");
17 List<WebElement> elements = driver.findElements(By.id("M"));
18 System.out.println("Number of elements:" +elements.size());
19
20 for(int i=0; i<elements.size(); i++){
21 System.out.println("Radio button text:" + elements.get(i).getAttribute("value"));
22 }
23 }
24}
通过多种策略访问塞伦基定位器
Selenium Webdriver 使用findElement(By.)
方法来引用 Web 元素. findElement 方法使用一个名为<"By">
的定位对象。
1、ID
命令: driver.findElement(By.id(
1WebElement user = driver.findElement(By.id("JournalDev"));
2、姓名
命令: driver.findElement(By.name(
1WebElement user = driver.findElement(By.name("JournalDev"));
3、班级名称
命令: driver.findElement(By.className(
1WebElement user = driver.findElement(By.className("JournalDev"));
四、链接文本
命令: driver.findElement(By.linkText()) 示例: JournalDev-1 JournalDev-2 Java示例代码以查找元素匹配的链接或部分链接文本:
1WebElement link = driver.findElement(By.linkText("JournalDev-1"));
2WebElement link = driver.findElement(By.partialLinkText("JournalDev-2"));
五、CSS Selector
命令: driver.findElement(By.cssSelector(
1WebElement emailText = driver.findElement(By.cssSelector("input#email"));
六、XPath
命令: driver.findElement(By.xpath(
1// Absolute path
2WebElement item = driver.findElement(By.xpath("html/head/body/table/tr/td"));
3
4// Relative path
5WebElement item = driver.findElement(By.xpath("//input"));
6
7// Finding elements using indexes
8WebElement item = driver.findElement(By.xpath("//input[2]"));
9```****