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元素。
Selenium WebDriver defines two methods for identifying the elements, they are findElement
and findElements
.
Selenium WebDriver定义了两种用于标识元素的方法,它们是findElement
和findElements
。
- findElement: This command is used to uniquely identify a web element within the web page. findElement :此命令用于唯一标识网页中的网页元素。
- 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 此命令用于访问网页上的任何单个元素
- 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 如果该元素在页面上不存在,则它将返回带有空列表的值