学习总结——Selenium元素定位
读一本好书,不能读读就算了,做一下总结,变成自己的,以备查阅。
1. driver.findElement(By.id(<element ID>))
ID是独一无二的,使用ID定位是最为推荐的方法。
但是:1.不是所有元素都会指定ID;2.有的ID属性的值是动态生成的。
2. driver.findElement(By.name(<element name>))
name属性不一定唯一,如果有多个,第一个出现的会被选择。
3. driver.findElement(By.className(<element class>))
4. driver.findElement(By.tagName(<htmltagname>))
tagName()方法是定位 HTML 标记名称:
WebElement table = driver.findElement(By.id("summaryTable"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
assertEquals(10, rows.size());
5. driver.findElement(By.linkText(<linktext>))
WebElement 类也可以支持查询子类元素。
WebElement topLink = driver.findElement(By.id("div1")).findElement(By.linkText("top"));
6. driver.findElement(By.partialLinkText(<linktext>))
当元素有部分在变时,可以用部分不变的内容来定位元素。
7. driver.findElement(By.cssSelector(<cssselector>))
1) 绝对路径如:WebElement userName = driver.findElement(By.cssSelector("html >
body > div > div > form > input"));
但是,这个策略会有一些的限制,他取决于页面的整个结构。如果有些许改变,选择 器将找不到这个元素。
相对路径
2) DOM中第一个<input>元素:WebElement userName = driver.findElement(By.cssSelector("input"));
3) 使用class定位:先指定一个 HTML 的标签,然后加一个“.”符号,跟上 class 属性的值, WebElement loginButton =driver.findElement(By.cssSelector("input.login"));可以找到按钮的<input>标签 class 为 login 的元素。
使用ID 来定位:先指定一个 HTML 标签,然后加上一个“#”符号,跟上 id 的属性 值,如下所示:
WebElement userName =driver.findElement(By.cssSelector("input#username"));
这将会返回 input 标签中 id 为 username 的元素。
使用name定位:WebElement userName =
driver.findElement(By.cssSelector("input[name=username]"));
使用 name 属性来定位元素和直接用 By 类中的 name()方法来定位相似。
使用其他的属性定位:WebElement previousButton driver.findElement(By.cssSelector("img[alt='Previous']"));
4) 使用多个属性来定位<input>元素:WebElement previousButton =driver.findElement(By.cssSelector("input[type='submit'][value='Login']"));
5) 使用属性名称定位元素:List<WebElement> imagesWithAlt =
driver.findElements(By.cssSelector("img[alt]"));
not()伪类匹配不满足规则的元素: 例如, 想要定位那些<img>标签中不含有alt属性,
List<WebElement> imagesWithoutAlt=driver.findElements(By.cssSelector("img:not([alt])"));
6) 部分属性值的匹配:
input[id^= ' ctrl']:以ctrl开始
input[id$='_userName']:以_userName结尾
input[id*='userName']:包含userName
8. driver.findElement(By.xpath(<xpath queryexpression>))
1) 绝对路径:WebElement userName =driver.findElement(By.xpath("html/body/div/div/form/input"));
这个策略有局限性,他需要参考整个页面的文档结构。如改变了,此元素的定位将会
失效。
相对路径:
2) DOM中第一个<input>元素:
WebElement userName = driver.findElement(By.xpath("//input"));
使用索引来定位元素,第二个<input>:
WebElement passwd = driver.findElement(By.xpath("//input[2]"));
3) 用 ID 属性来定位:
WebElement userName =driver.findElement(By.xpath("//input[@id='username']"));
使用 alt 属性来定位:
WebElement previousButton = driver.findElement(By.xpath("img[@alt='Previous']"));
4) 使用多个属性来定位<input>元素:
- WebElement previousButton =driver.findElement(By.xpath("//input[@type='submit'][@value='Login']"));
- WebElement previousButton = driver.findElement(By.xpath("//input[@type='submit'and @value='Login']"));
- WebElement previousButton = driver.findElement(By.xpath("//input[@type='submit'or @value='Login']"));
5) 使用 XPath 及属性名称定位元素:
List<WebElement> imagesWithAlt = driver.findElements(By.xpath ("img[@alt]"));
6) 部分属性值的匹配:
input[starts-with(@id,'ctrl')]:id以ctrl开始
input[ends-with(@id,'_userName')]:id以_userName结束
Input[contains(@id,'userName')]:id包含userName
7) 使用值来匹配任意属性及元素:
WebElement userName = driver.findElement(By.xpath("//input[@*='username']"));
8) 使用 XPath 轴来定位元素:用到再研究。
总结提示:
- 使用 id,name 或 class 属性是定位元素的首选方法。
- CSS 选择器和 XPath 在 Selenium 用户中非常流行,但是 CSS 选择器相比 XPath 从难易、速度、效率来说更为推荐大家使用。
- XPath 的查询慢于CSS 选择器,因为 XPath支持双向的查询。可以通过元素的父,兄弟,子节点来定位元素。
学习总结——Selenium元素定位的更多相关文章
- UI自动化学习笔记- Selenium元素定位及元素操作
一.元素定位 1. 如何进行元素定位? 元素定位就是通过元素的信息或元素层级结构来定位元素的 2.定位工具 浏览器开发者工具 3.元素定位方式 Selenium提供了八种定位元素方式 id name ...
- python学习之——selenium元素定位
web自动化测试按步骤拆分,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告. 其中定位元素尤为关键,此篇是使用webdriver通过页面各个 ...
- Selenium3 + Python3自动化测试系列二——selenium元素定位
一.selenium元素定位 Selenium对网页的控制是基于各种前端元素的,在使用过程中,对于元素的定位是基础,只有准去抓取到对应元素 才能进行后续的自动化控制,我在这里将对selenium8种元 ...
- selenium元素定位之css选择器
在selenium元素定位时会用到css选择器选取元素,虽说xpath在定位元素时能解决大部分问题,但使用css选择器选取元素也是一种不错的选择. css相较与xpath选择元素优点如下: 表达式更加 ...
- selenium元素定位陷阱规避
为什么selenium可以在各个浏览器上运行?因为selenium在与各个浏览器驱动执行前,会先把脚本转化成webdriver, webdriver wire协议(一种json格式的协议),这样就与脚 ...
- 自动化测试基础篇--Selenium元素定位
摘自https://www.cnblogs.com/sanzangTst/p/7457111.html 一.Selenium元素定位的重要性: Web自动化测试的操作:获取UI页面的元素,对元素进行操 ...
- 我是这么学习Selenium元素定位操作的
写在前面 做web自动化测试都有体会,本质也就是通过操作页面元素对象来模拟用户操作行为,那么首先我们先找到这些元素对象,然后才能进行一系列操作. 我们得先告诉自动化工具或者说代码要操作那个元素,毕竟代 ...
- selenium元素定位学习笔记
一,定位原则 稳定 简单灵活 唯一 WebDriver提供了两种方式来定位页面元素,分别是find_element_by_XXX和find_elements_by_XXX.第一种方式的结果是在正常情况 ...
- python selenium 元素定位(三)
上两篇的博文中介绍了python selenium的环境搭建和编写的第一个自动化测试脚本,从第二篇的例子中看出来再做UI级别的自动化测试的时候,有一个至关重要的因素,那就是元素的定位,只有从页面上找到 ...
随机推荐
- java 运算符使表达式结果类型自动提升
1.表达式中的自动类型提升: 表达式求值时,Java自动的隐含的将每个byte.short或char操作数提升为int类型,这些类型的包装类型也是可以的. 例如:short s1 = 1; s1 = ...
- 将网页另存为PDF文件的方法
使用google chrome浏览器测试,其他浏览器应该也是差不多的方法. 步骤1: 打开需要转换的网页: 步骤2: 点击右上角的三点按键,或者快捷键Ctrl+P,调用的打印页面: 步骤3: 选择目标 ...
- how to build apache log4cxx 0.10 by Visual Studio 201*
Chapter 1 Official Steps We are going to follow the steps here, http://logging.apache.org/log4cxx/b ...
- 有关默认相机转VR相机
呃...15年开篇~ 去年想写一个有关默认相机转VR相机的脚本,当时没写完,今天不小心翻到并写完了,而且思路也和原来完全不一样了,增加了是否删除原相机与是否转换所选相机的选项. 由于国内VR版本比较混 ...
- python之购物车的编写(熬夜撸代码中。。。)
购物车的编写对于我这种不是很精通函数的小白来说,简直太难了.各种坑,各种无奈啊!不过总算也是写出来了! 不多说,直接上代码! #!/usr/bin/env python#用户名 sanjiang#密码 ...
- android development
1. Supporting different devices 1) Supporting different screen size 主要有几点,首先是布局文件夹以及布局文件的命名 layout/m ...
- flask在windows上用mod_wsgi部署
flask在windows上用mod_wsgi部署也是折腾了不少时间,下面就总结下. 首先下载Apache httpd,我认为Apache Hans比较好: 一般这种情况下,你的python环境已经安 ...
- [转]MySQL Connector/C++(一)
http://www.cnblogs.com/dvwei/archive/2013/04/18/3029464.html#undefined#undefined MySQL Connector/C++ ...
- [Spring] - Quartz定时任务 - Annotation
Spring + Quartz可以使用annoation方式: 1.AppJob类: package com.my.quartz.testquartz1; import org.springframe ...
- IIS7.5打开GZip压缩,同时启用GZip压缩JS/CSS文件的设置方法[bubuko.com]
IIS7.5或者IIS7.0开启GZip压缩方法:打开IIS,在右侧点击某个网站,在功能视图中的“IIS”区域,双击进入“压缩”,如图下图: 分别勾选“启用动态内容压缩”和“启用静态内容压缩”.这样最 ...