转自:http://blog.sina.com.cn/s/blog_6966650401012a6u.html

WebDriver拾级而上·之三 定位页面元素

selenium-webdriver提供了强大的元素定位方法,支持以下三种方法:
         1. 单个对象的定位方法
         2. 多个对象的定位方法
         3. 层级定位

注意:

selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素 List,如果不存在符合条件的就返回一个空的list。
 
一、定位单个元素
A.使用className进行定位
当所定位的元素具有class属性的时候我们可以通过classname来定位该元素。
例:下面的例子定位页面上class为"username"的li。
    WebElement element = driver.findElement(By.className("username"));
    System.out.println(element.getTagName());

输出结果:Li

B.使用id属性定位
例:<input id="passport_user" type="text" value="" title="用户名/邮箱" name="passport_user">
     WebElement element = dr.findElement(By.id("passport_user"));
     System.out.println(element.getAttribute("title"));

输出结果:用户名/邮箱

C.使用name属性定位
例:<input id="passport_user" type="text" value="" title="用户名/邮箱" name="passport_user">
     WebElement e = dr.findElement(By.name("passport_user"));  
 
D.使用css属性定位
例:<input id="passport_user" type="text" value="" title="用户名/邮箱" name="passport_user">
     WebElement e1 = dr.findElement(By.cssSelector("#passport_user"));

 
详解:
1.css之后代选择器
<p>
    <em>location1</em>
</p>
<ol>
     <li><em>location2</em></li>
</ol>
可以通过css=p em这个可以选中文本为location1的em元素
css=ol em这个可以选中文本为location2的em元素
css后代选择器和xpath中//div//a一样:取得所有div下面所有的a节点。这个是忽略了父子节点
 
 
<div>
    <p><em>location1</em></p>
</div>
<div>
<ol>
    <li><strong><em>location2</em></strong></li>
    <li><em>location3</em></li>
    <li><em>location4</em></li>
</ol>
</div>
可以通过css=p>em来定位location1
css之父子节点选择器给后代选择器加了一个限制,类似xpath中//div/p/em:所有div下的子元素p的子元素em。
css=li+li em来定位location3,location4的em
css=li+strong+em来定位文本为location2的em
 
2.css之id选择器
<input id="location1" type="button"/>
<input id="location2" type="radio"/>
通过css=#location1来定位type为button的按钮
通过css=#location2来定位type为radio的单选框
 
3.css之类选择器
<input class="location1" type="button" value="确定"/>
<input class="location2" type="button" value="取消"/>
通过css=input.location1来选择value值为确定的按钮
通过css=input.location2来选择value值为取消的按钮
 
4.css之属性选择器
<input class="location1" type="button" value="确定"/>
<input class="location2" type="button" />
通过css=[class=location1]可以定位第一个按钮
通过css=[class~=1]可以定位第一个按钮
通过css=[value="确定"]可以定位第一个按钮
通过css=input[class="location"]可以定位第二个按钮
 
E.按标记(tag)名称查找
元素的DOM标记名称
<iframe src="..."></iframe>
WebElement frame = driver.findElement(By.tagName("iframe"));
 
F.按链接文本查找
<a href="http://www.google.com/search?q=cheese">cheese</a>>
WebElement cheese = driver.findElement(By.linkText("cheese"));
 
按部分链接文本查找
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
 
G.使用 XPATH定位

例:<input id="passport_user" type="text" value="" title="用户名/邮箱" name="passport_user">
    WebElement element =dr.findElement(By.xpath("//input[@id='passport_user']"));

 
parent::返回父节点
following::返回此节点后面的兄弟节点
preceding::返回此节点前面的兄弟节点
 
div>
<input id="location1" type="button" />
<input type="button" />
</div>
 
通过id为location1可以很随意的定位到兄弟节点
//div/input[@id='location1']/following::input
 
也可以通过location1很随意的定位到父类节点div。
//div/input[@id='location1']/parent::div。
 
也可以通过索引为2的input定位到id为location1的input
//div/input[2]/preceding-sibling::input
 

有几个非常有用的Firefox插件,有助于发现一个元素的XPath:

XPath Checker - suggests XPath and can be used to test XPath results.
Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.
XPath Checker - 建议XPath并可以用于测试XPath的结果
Firebug - XPath建议仅仅是这非常有用的插件的许多强有力特征中的一个。
 
 

二、定位多个元素
//定位到所有<input>标签的元素,然后输出他们的id
    List<WebElement> element = driver.findElements(By.tagName("input"));
    for (WebElement e : element)
        System.out.println(e.getAttribute("id"));

三、层级定位
层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。
层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。

下面的代码演示了如何使用层级定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的文本
//定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的值
    WebElement element = driver.findElement(By.className("login"));
    List<WebElement> el = element.findElements(By.tagName("label"));
    for(WebElement e : el)
        System.out.println(e.getText());

 
四、使用Javascript
你可以执行任何Javascript,以找到一个元素,只要你找到一个DOM元素,它将自动转换为WebElement对
象。
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return
$('.cheese')[0]");

selenium By 元素定位详解的更多相关文章

  1. 『心善渊』Selenium3.0基础 — 4、Selenium基础元素定位详解

    目录 1.什么是元素定位 2.Selenium元素定位常用API (1)By_id 定位 (2)by_name 定位 (3)by_class_name 定位 (4)by_tag_name 定位 (5) ...

  2. selenium:css_selector定位详解

    selenium:css_selector定位详解(css selector和xpath的比较) 来源:https://www.cnblogs.com/haifeima/p/10138154.html ...

  3. selenium:Xpath定位详解

    xpath定位在业界被戏称为元素定位的"屠龙宝刀",宝刀在手,武林我有.现在我们就来详解xpath定位方法. 一.xpath通过元素属性定位 xpath可以通过元素的属性来定位,如 ...

  4. selenium:css_selector定位详解(css selector和xpath的比较)

    selenium使用css selector和xpath的比较 selenium提供的定位方式(常用) ID NAME CLASS CSS SELECTOR XPATH   推荐的定位方式的优先级 优 ...

  5. selenium界面元素定位

    一.        Selenium界面元素定位 本文元素定位以das2为例 #导入包 from selenium import  webdriver #打开火狐驱动 driver=webdriver ...

  6. 小甲鱼PE详解之基址重定位详解(PE详解10)

    今天有一个朋友发短消息问我说“老师,为什么PE的格式要讲的这么这么细,这可不是一般的系哦”.其实之所以将PE结构放在解密系列继基础篇之后讲并且尽可能细致的讲,不是因为小甲鱼没事找事做,主要原因是因为P ...

  7. 带你走进CSS定位详解

    学习CSS相关知识,定位是其中的重点,也是难点之一,如果不了解css定位有时候都不知道怎么用,下面整理了一下关于定位属性的具体理解和应用方案. 一:定位 定位属性列表 position top bot ...

  8. CSS进阶内容—浮动和定位详解

    CSS进阶内容-浮动和定位详解 我们在学习了CSS的基本知识和盒子之后,就该了解一下网页的整体构成了 当然如果没有学习之前的知识,可以到我的主页中查看之前的文章:秋落雨微凉 - 博客园 CSS的三种布 ...

  9. CSS中伪类及伪元素用法详解

    CSS中伪类及伪元素用法详解   伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...

随机推荐

  1. AltiumDesigner学习笔记(一)——创建工程与原理图文件

    一.创建工程与原理图文件 1.通过菜单创建PCB工程 (1)File - New - Project - PCB Project,即可在当前工作区创建新的PCB工程 (2)新建工程并不直接在硬盘中创建 ...

  2. 添加网页ico

    <link rel="Shortcut Icon" href="/favicon.ico" />

  3. 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo

    杂家前文是在2012年的除夕之夜仓促完成,后来很多人指出了一些问题,琐事缠身一直没有进行升级.后来随着我自己的使用,越来越发现不出个升级版的demo是不行了.有时候就连我自己用这个demo测一些性能. ...

  4. html 绘制图像

  5. redis的相关知识

    1. 依赖包安装 pom.xml 加入: <!-- redis cache related.....start --> <dependency> <groupId> ...

  6. ural 1217. Unlucky Tickets

    1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each ...

  7. Rain on your Parade

    Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Ot ...

  8. 转 Web移动应用调试工具——Weinre

    如今人们也越来越习惯在手机上浏览网页,而在手机上这些针对桌面浏览器设计的网页经常惨不忍睹.Web应用开发者需要针对手机进行界面的重新设计,但是手机上并没有称心如意的调试工具(如Firebug.web ...

  9. SOLR (全文检索)

    SOLR (全文检索) http://sinykk.iteye.com/ 1.   什么是SOLR 官方网站 http://wiki.apache.org/solr http://wiki.apach ...

  10. [开源框架推荐]Icepdf:纯java的pdf文档的提取和转换库

    ICEpdf 是一个轻量级的开源 Java 语言的 PDF 类库.通过 ICEpdf 可以用来浏览.内容提取和转换 PDF 文档,而无须一些本地PDF库的支持. 可以用来做什么? 1.从pdf文件中提 ...