Selenium 中的 findElement 和 findElements:带有示例的用例
当我们想要与任何网页交互时,我们使用定位器来识别 Web 元素,并使用定位器与它们交互。我们在网页上使用 HTML 元素,同时使用 Selenium WebDriver 自动化任何基于 Web 的应用程序。
Selenium WebDriver 有两种识别Web元素的方法,分别是findElement和findElements。
**1. findElement:**用于唯一地标识网页内的任何网络元素。
**2. findElements:**用于唯一标识网页内的网页元素列表。
这些方法对于在网页上定位网页元素非常重要,但它们的用途不同,并且具有不同的用例。
让我们看看这两种方法到底是什么:
**1. 查找元素():**
**以下是 findElement 命令的语法:**
WebElement 元素名称 = 驱动程序.findElement(By.LocatorStrategy("LocatorValue"));
定位器策略由任意一个定位器组成。定位器值是标识 Web 元素的唯一值。
**2.查找元素():**
Java 中的语法:
**以下是 findElements 命令的语法**
列表元素名称 = 驱动程序.findElements(By.LocatorStrategy("LocatorValue"));
定位器策略由任意一个定位器组成。定位器值是标识 Web 元素的唯一值。
现在让我们看看这两种方法的区别:
Selenium 中 find Element 和 find Elements 之间的区别

现在让我们看看以下每种方法的用例:
如何使用 Selenium findElement

首先让我们看看如何使用 Selenium findElement 方法:
在上面的截图中,我们正在寻找 Google 搜索文本框的唯一定位器。
如您所见,它具有唯一的 ID,使用该 ID,我们创建了如下所示的 Xpath:
//文本区域[@id='APjFqb']
下面是 Java 代码片段:
驱动程序.get(“https://www.google.com/”)
WebElement元素 = 驱动程序.findElement(By.xpath("//textarea[@id='APjFqb']"));
如果在页面上找不到匹配的元素,则会出现NoSuchElementException。
**现在让我们看看 Selenium Java 中 findElements 方法的用例:**

在上面的截图中,对于 IRCTC 应用程序的注册表,我们有一个定位器类,其值为“col-xs-12 inputBoxPad”,它与网页上的 8 个 Web 元素匹配。
以下是 Java Selenium 中的代码:
驱动程序.get(“https://www.irctc.co.in/nget/profile/user-signup”);
列表元素 = 驱动程序.findElements(By.className(“col-xs-12 inputBoxPad”));
System.out.println("元素数量:" +elements.size());
对于(int i = 0; System.out.println("输入字段:" + elements.get(i).getAttribute("value")); 使用 findElement 方法的多个定位器方法: Selenium WebDriver 提供了 findElement(By) 方法来定位脚本中的 Web 元素并与之交互。 find Element 方法使用定位器对象“By”。 根据需要,有多种方法可以将“By”与定位器结合使用。 按 ID: 驱动程序.findElement(By.id()) 通过 Xpath: 驱动程序.findElement(By.xpath()) 按名称: 驱动程序.findElement(By.name()) 按类别名称: 驱动程序.findElement(By.className(“”)) 通过 CSS 选择器: 驱动程序.findElement(By.cssSelector()) 通过 LinkText: driver.findElement(By.linkText()) 结论 了解 SeleniumWebdriver Java 中 findElement() 和 findElements() 之间的区别至关重要。这两种方法用途不同,在自动化基于 Web 的应用程序时具有独特的用例。 简而言之,findElement() 用于定位并与单个元素交互,抛出异常信息元素匹配。findElements() 用于定位多个元素,如果没有匹配,则返回空列表。
System.out.println("Input field:" + elements.get(i).getAttribute("value"));
Multiple Locator Methods Using findElement Method:
Selenium WebDriver provides the findElement(By) method to locate and interact with web elements in a script. The find Element method uses the locator object “By”. There are many ways of using “By” with locators based on the requirement.
driver.findElement(By.id())
driver.findElement(By.xpath())
driver.findElement(By.name())
driver.findElement(By.className(“”))
driver.findElement(By.cssSelector())
driver.findElement(By.linkText())
Conclusion
understanding the differences between findElement() and findElements() in SeleniumWebdriver Java is essential. These two methods have different purposes and have unique use cases while automating web based applications.
In brief, findElement() is used to locate and interact with a single element, throwing an exception info Element is matching. findElements() is used to locate multiple elements, returning an empty list if none are matching.