selenium 示例_Selenium findElement和findElements示例

本文介绍了Selenium WebDriver中findElement和findElements两种定位网页元素的方法。findElement用于唯一标识单个元素,当无法找到元素时会抛出NoSuchElementException。而findElements则返回元素列表,如果找不到元素则返回空列表。文章还提供了各种定位策略的示例,如按ID、名称、类名、LinkText、XPath等。
摘要由CSDN通过智能技术生成

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定义了两种用于标识元素的方法,它们是findElementfindElements

  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

    此命令用于访问网页上的任何单个元素
  • 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命令

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值