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命令 (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.

定位器策略可以是以下任意值。

  • IDID
  • Name名称
  • Class Name班级名称
  • Tag Name标签名
  • Link Text连结文字
  • Partial Link Text部分链接文字
  • XPathXPath

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:

以下应用程序用于演示目的:

https://www.irctc.co.in/nget/user-registration

https://www.irctc.co.in/nget/user-registration

Scenario

情境

  1. Open the https://www.irctc.co.in/nget/user-registration for AUT打开AUT的https://www.irctc.co.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\\chromedriver.exe");WebDriver driver= new ChromeDriver();driver.manage().window.maximize():driver.get(:"https://www.irctc.co.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

以下应用程序用于演示目的

https://www.irctc.co.in/nget/user-registration

https://www.irctc.co.in/nget/user-registration

Scenario

情境

  1. Open the https://www.irctc.co.in/nget/user-registration for AUT打开AUT的https://www.irctc.co.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\\chromedriver.exe");WebDriver driver= new ChromeDriver();driver.get("https://www.irctc.co.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:" + elements.get(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.com”>
<input class=”btn btn-small” type=”submit” value=”Subscribe to blog>

范例
<input class =“ email” id =“电子邮件” type =“文本”占位符=“ xxx@email.com”>
<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"));

6.通过XPath (6. By XPath)

Command: driver.findElement(By.xpath(<xpath>))

命令 :driver.findElement(By.xpath(<xpath>))

Java example code for XPath:

XPath的Java示例代码

// Absolute path
WebElement item = driver.findElement(By.xpath("html/head/body/table/tr/td"));// Relative path
WebElement item = driver.findElement(By.xpath("//input"));// Finding elements using indexes
WebElement item = driver.findElement(By.xpath("//input[2]"));

翻译自: https://www.journaldev.com/29950/selenium-findelement-findelements-examples

selenium 示例

selenium 示例_Selenium findElement和findElements示例相关推荐

  1. __init__在python中的用法知乎_python使用selenium爬虫知乎的方法示例

    说起爬虫一般想到的情况是,使用 python 中都通过 requests 库获取网页内容,然后通过 beautifulSoup 进行筛选文档中的标签和内容.但是这样有个问题就是,容易被反扒机制所拦住. ...

  2. python自动化简历_python+selenium实现简历自动刷新的示例代码

    本文用到的文件的下载地址 百度网盘链接: https://pan.baidu.com/s/1tmpdEfAZKff5TOMAitUXqQ 提取码: e6at 1 安装Python 和 selenium ...

  3. python selenium爬虫实例_python使用selenium爬虫知乎的方法示例

    说起爬虫一般想到的情况是,使用 python 中都通过 requests 库获取网页内容,然后通过 beautifulSoup 进行筛选文档中的标签和内容.但是这样有个问题就是,容易被反扒机制所拦住. ...

  4. FindElement和FindElements命令

    接下来要做的是与应用程序的不同元素进行交互,因此需要在交互之前首先定位元素.定位元素可以在AndroidDriver 实例本身上完成,因为它为我们提供了" Find Element &quo ...

  5. 【Android 内存优化】Bitmap 硬盘缓存 ( Google 官方 Bitmap 示例 | DiskLruCache 开源库 | 代码示例 )

    文章目录 一.Google 官方 Bitmap 相关示例参考 二.磁盘缓存类 DiskLruCache 三.磁盘缓存初始化 四.存储数据到磁盘缓存中 五.从磁盘缓存中读取数据 六. Android 1 ...

  6. 【集合论】等价类 ( 等价类概念 | 等价类示例 | 等价类性质 | 商集 | 商集示例 )★

    文章目录 一.等价类 二.等价类示例 三.等价类性质 四.商集 五.商集示例 1 六.商集示例 2 七.商集示例 3 一.等价类 RRR 关系 是 AAA 集合 上的二元关系 , AAA 集合不为空集 ...

  7. selenium 示例_Selenium测试中所需的功能和示例

    selenium 示例 所需功能是用于声明一组基本要求(例如浏览器,操作系统,浏览器版本等的组合)的类,以对Web应用程序执行自动跨浏览器测试. 当我们尝试通过Selenium自动化测试来自动化测试脚 ...

  8. 量角器中Selenium定位器的完整指南(示例)

    在测试网站的功能时,特别是Web元素(例如单选按钮,文本框,下拉列表等),您需要确保能够访问这些元素. Selenium定位器正是出于这个目的,通过使用此命令,我们可以识别这些Web元素DOM(文档对 ...

  9. java selenium iframe_java selenium处理Iframe中的元素示例

    java selenium  处理Iframe 中的元素 有时候我们定位元素的时候,发现怎么都定位不了. 这时候你需要查一查你要定位的元素是否在iframe里面 阅读目录 什么是iframe ifra ...

最新文章

  1. 十个经典的学生上课插嘴ZT
  2. Windows 根据进程名杀死进程 kill
  3. 红绿灯问题(位运算)
  4. Linux--内存结构
  5. Vim编辑器的基本使用和三种模式
  6. 智能机器人服务广州春运
  7. C# 多线程 Parallel.For 和 For 谁的效率高?那么 Parallel.ForEach 和 ForEach 呢?
  8. 基于ASP.NET Core api 的服务器事件发送
  9. python利用faker,输出企业名称、用户名称、手机号、地址信息等测试数据实例
  10. [转]ubuntu apt错误解决办法
  11. 单图说TDSQL;OceanBase 2.2 事务引擎核心功能;穿云箭2.0版发布;RMAN DUPLICATE配置19C DG;外键上有无索引的影响;MySQL8.0 索引新功能;GaussDB C
  12. Web API-基本认知
  13. C 字符串转换为c语言字符串,OC字符串与C语言字符串之间的相互转换
  14. editplus更改编码
  15. 《Effective Java 3rd》读书笔记——对于所有对象都通用的方法
  16. python源代码(create,huafen,doc)
  17. watir 基础知识
  18. 局部语义地图构建——HDMapNet
  19. 第46讲 Android Camera2 API AWB自动白平衡
  20. mac 文字识别软件ocr_Mac上那些好用的文字识别工具

热门文章

  1. 【BZOJ-1502】月下柠檬树 计算几何 + 自适应Simpson积分
  2. ZC_汇编指令_cmp
  3. iOS ChildViewController与View
  4. UIPickerView用法(左右比例,整体大小,字体大小)
  5. 个人开源项目之快速检索算法
  6. !DOCTYPE标签的定义与用法
  7. 如何对ASP.NET进行性能优化
  8. [转载] Python中三种类型的引号(单引号、双引号、三引号)
  9. [转载] Python 完整实现的简单遗传算法(SGA)
  10. Android 自定义RecyclerView 实现真正的Gallery效果