读一本好书,不能读读就算了,做一下总结,变成自己的,以备查阅。

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 轴来定位元素:用到再研究。

总结提示:

  1. 使用 id,name 或 class 属性是定位元素的首选方法。
  2. CSS 选择器和 XPath 在 Selenium 用户中非常流行,但是 CSS 选择器相比 XPath 从难易、速度、效率来说更为推荐大家使用。
  3. XPath 的查询慢于CSS 选择器,因为 XPath支持双向的查询。可以通过元素的父,兄弟,子节点来定位元素。

学习总结——Selenium元素定位的更多相关文章

  1. UI自动化学习笔记- Selenium元素定位及元素操作

    一.元素定位 1. 如何进行元素定位? 元素定位就是通过元素的信息或元素层级结构来定位元素的 2.定位工具 浏览器开发者工具 3.元素定位方式 Selenium提供了八种定位元素方式 id name ...

  2. python学习之——selenium元素定位

    web自动化测试按步骤拆分,可以分为四步操作:定位元素,操作元素,获取返回结果,断言(返回结果与期望结果是否一致),最后自动出测试报告. 其中定位元素尤为关键,此篇是使用webdriver通过页面各个 ...

  3. Selenium3 + Python3自动化测试系列二——selenium元素定位

    一.selenium元素定位 Selenium对网页的控制是基于各种前端元素的,在使用过程中,对于元素的定位是基础,只有准去抓取到对应元素 才能进行后续的自动化控制,我在这里将对selenium8种元 ...

  4. selenium元素定位之css选择器

    在selenium元素定位时会用到css选择器选取元素,虽说xpath在定位元素时能解决大部分问题,但使用css选择器选取元素也是一种不错的选择. css相较与xpath选择元素优点如下: 表达式更加 ...

  5. selenium元素定位陷阱规避

    为什么selenium可以在各个浏览器上运行?因为selenium在与各个浏览器驱动执行前,会先把脚本转化成webdriver, webdriver wire协议(一种json格式的协议),这样就与脚 ...

  6. 自动化测试基础篇--Selenium元素定位

    摘自https://www.cnblogs.com/sanzangTst/p/7457111.html 一.Selenium元素定位的重要性: Web自动化测试的操作:获取UI页面的元素,对元素进行操 ...

  7. 我是这么学习Selenium元素定位操作的

    写在前面 做web自动化测试都有体会,本质也就是通过操作页面元素对象来模拟用户操作行为,那么首先我们先找到这些元素对象,然后才能进行一系列操作. 我们得先告诉自动化工具或者说代码要操作那个元素,毕竟代 ...

  8. selenium元素定位学习笔记

    一,定位原则 稳定 简单灵活 唯一 WebDriver提供了两种方式来定位页面元素,分别是find_element_by_XXX和find_elements_by_XXX.第一种方式的结果是在正常情况 ...

  9. python selenium 元素定位(三)

    上两篇的博文中介绍了python selenium的环境搭建和编写的第一个自动化测试脚本,从第二篇的例子中看出来再做UI级别的自动化测试的时候,有一个至关重要的因素,那就是元素的定位,只有从页面上找到 ...

随机推荐

  1. nodejs--模块

    在客户端可以将所有的javascript代码分割成几个JS文件,然后在浏览器中将这些JS文件合并.但是在nodejs中是通过以模块为单位来划分所有功能的.每一个模块为一个JS文件,每一个模块中定义的全 ...

  2. 初识Linux-3

    1,find 目录 条件 [处理命令]-默认是print操作 2,find d6 -name "*cpp" -exec rm{} ";"(或者‘;’或者/;都是 ...

  3. Codeforces Round #168 (Div. 2)

    A. Lights Out 模拟. B. Convex Shape 考虑每个黑色格子作为起点,拐弯次数为0的格子构成十字形,拐弯1次的则是从这些格子出发直走达到的点,显然需要遍历到所有黑色黑色格子. ...

  4. Lua 自定义函数string.split

    function string.split(str, delimiter)    if str==nil or str=='' or delimiter==nil then        return ...

  5. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. C语言字符串操作总结大全(超详细)

    本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat( ...

  7. c# List的排序

    list 是我们常用到的数据类型,我们常常会用list去处理很多的数据.我们也常常会有这样的一个操作,就是排序sort list 所在的命名空间是System.Collections.Generic ...

  8. linux下jdk的安装(tar包)

    1.查看jdk安装路径 [root@localhost ~]# whereis javajava: /usr/bin/java /etc/java /usr/lib/java /usr/share/j ...

  9. golang: 根据json生成go源文件

    https://github.com/ChimeraCoder/gojson $ git clone https://github.com/ChimeraCoder/gojson.git$ cd go ...

  10. linux 安装 python2.7

    若新安装虚拟机,或者新装linux系统.需安装gcc等yum -y install gcc gcc-c++ autoconf automake cmake ntp rsync ssh vim  yum ...