转自: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. Ubuntu各版本下载地址

    Ubuntu各版本下载地址:     http://old-releases.ubuntu.com/releases/

  2. js拆分数组

    <script language="javascript"> str="2,2,3,5,6,6"; //这是一字符串 var strs= new A ...

  3. node.js打开浏览器

    通过nodejs的child_process识别环境, 用不同的CLI打开默认浏览器: var child_process = require("child_process"); ...

  4. spring获取webapplicationcontext,applicationcontext几种方法详解

    法一:在初始化时保存ApplicationContext对象代码: ApplicationContext ac = new FileSystemXmlApplicationContext(" ...

  5. SU Demos-02Filtering-02Subfilt

    巴特沃斯滤波器的特点是通频带的频率响应曲线最平滑.这种滤波器最先由英国工程师斯替芬·巴特沃斯(Stephen Butterworth)在1930年发表在英国<无线电工程>期刊的一篇论文中提 ...

  6. VMware12版虚拟机怎么安装win7系统

    工具/原料   VMware workstation 12 windows7镜像ios系统文件 链接:http://pan.baidu.com/s/1c0YrDgG 密码:vna1 建立一个新的虚拟机 ...

  7. List<T>的使用

    定义:List<T>类表示可通过索引访问的对象的强类型列表,提供用于对列表进行搜索.排序和操作的方法. 作用:泛型最常见的用途是泛型集合我们在创建列表类时,列表项的数据类型可能是int,s ...

  8. CodeForces Round 193 Div2

    I know,this is another failure.It seems like that failure is a habit with me.This time I solved two ...

  9. Hook to function

    myFun.h 1: #include <stdio.h> 2:  3: void __cyg_profile_func_enter(void *this_fn, void *call_s ...

  10. 升级到WP8必需知道的13个特性

    http://www.cnblogs.com/sonic1abc/archive/2012/11/28/2792467.html   Windows phone 8 SDK 已经发布一段时间了, 已经 ...