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

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. C++ 中静态成员函数访问非静态成员变量的方法

    最近在 VS2010 里开发出厂烧写工具,遇到一个问题: 我创建了一个线程,在这个线程里要访问非静态成员,而这个线程函数是静态的.最后找到的办法是用对象指针来做. sourcecode: #test. ...

  2. Zend Studio 中安装emmet插件的方法

    本人的Zend Studio版本是Zend Studio 10.0.0. 1. 打开Zend Studio,点击 Help --> Install New Software,如下图: 2.  在 ...

  3. 自动运维:Ansible -ansible tower

    文档主页:http://docs.ansible.com/参考文档:http://docs.ansible.com/ansible/参考文档:http://docs.ansible.com/ansib ...

  4. Oracle用户密码过期后重置SYS用户密码

    问题状况: SYS.SYSTEM用户的密码过期,无法登陆. 运行EM控制台后,出现错误——ORA-28001: the password has expired (DBD ERROR: OCISess ...

  5. atom 调用g++编译cpp文件

    atom 有script插件可以直接调用很多编译器,试了很多次一直出问题找不到文件,查了文档之后发现script只支持OSX和linux. 所以,还是使用gpp-compiler比较靠谱 gpp-co ...

  6. Eclipse插件安装方式及使用说明

    拷贝安装方式 1.通过ECLIPSE_HOME\plugins安装 在eclipse的主目录ECLIPSE_HOME, 比如在我的机器上安装的目录是:ECLIPSE_HOME有一个plugins的目录 ...

  7. oracle中找出某个字段中有非数字型的记录

    工作中遇到一个大表记录中有非法非数字字符,不想用正则语法去做, 用一条SQL语句查出来的方法如下: select * from table where translate(col,'*01234567 ...

  8. three.js学习笔记

    一.名词解释 scene - 场景 camera - 摄影机 renderer - 渲染器:描绘器 Vector - 三维向量的对象 orthographic - 正射 field of view - ...

  9. SpringMVC使用的几个要点

    1.使用 @RequestParam("username") 来对应参数名的时候,这个参数必须要传入,否则会报错.没加@RequestParam则可传可不传 @RequestMap ...

  10. 浏览器内核控制Meta标签

    国内的主流浏览器都是双核浏览器:基于Webkit内核用于常用网站的高速浏览.基于IE的内核用于兼容网银.旧版网站.以360的几款浏览器为例,我们优先通过Webkit内核渲染主流的网站,只有小量的网站通 ...