在实际的项目测试中,经常会有这样的需求:页面上有很多个属性基本相同的元素 ,现在需要具体
定位到其中的一个。由于属性基本相当,所以在定位的时候会有些麻烦,这时候就需要用到层级定位。先
定位父元素,然后再通过父元素定位子孙元素。
level_locate.html
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Level Locate</title>
<script type="text/javascript" async="
" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet" />
</head>
<body>
<h3>Level locate</h3>
<div class="span3">
<div class="well">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Link1</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel" id="dropdown1" >
<li><a tabindex="-1" href="#">Action</a></li>
<li><a tabindex="-1" href="#">Another action</a></li>
<li><a tabindex="-1" href="#">Something else here</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
<div class="span3">
<div class="well">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Link2</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel" >
<li><a tabindex="-1" href="#">Action</a></li>
<li><a tabindex="-1" href="#">Another action</a></li>
<li><a tabindex="-1" href="#">Something else here</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
</body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>
将上面的代码保存为 level_locate.html,通过浏览器打开将看到以下页面。

图 3.6
通过对上面代码的分析,发现两个下拉菜单中每个选项的 link text 都相同,href 也一样,所以在这
里就需要使用层级定位了。
具体思路是:先点击显示出 1 个下拉菜单,然后再定位到该下拉菜单所在的 ul,再定位这个 ul 下的
某个具体的 link 。在这里,我们定位第 1 个下拉菜单中的 Another action 这个选项。
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import time
import os
driver = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('level_locate.html')
driver.get(file_path)
#点击 Link1 链接(弹出下拉列表)
driver.find_element_by_link_text('Link1').click()
#在父亲元件下找到 link 为 Action 的子元素
menu =
driver.find_element_by_id('dropdown1').find_element_by_link_text('Another
action')
#鼠标移动到子元素上
ActionChains(driver).move_to_element(menu).perform()
time.sleep(5)
driver.quit()
driver.find_element_by_id('xx').find_element_by_link_text('xx').click()
这里用到了二次定位,通过对 Link1 的单击之后,出现下拉菜单,先定位到下拉菜单,再定位下拉菜
单中的选项。当然,如果菜单选项需要单击,可通过二次定位后也直接跟 click()操作。
ActionChains(driver)
driver: wedriver 实例执行用户操作。
ActionChains 用于生成用户的行为;所有的行为都存储在 actionchains 对象。通过 perform()执行存储的
行为。
move_to_element(menu)
move_to_element 方法模式鼠标移动到一个元素上,上面的例子中 menu 已经定义了他所指向的是哪一
个元素。
perform()
执行所有 ActionChains 中存储的行为。
博客园---虫师
定位后的效果如下,鼠标点击 Link1 菜单,鼠标移动下 Another action 选项上,选项出现选中色。

图 3.7

转:python webdriver API 之层级定位的更多相关文章

  1. 转:python webdriver API 之简单对象的定位

    对象(元素)的定位和操作是自动化测试的核心部分,其中操作又是建立在定位的基础上的,因此元素定位就显得非常重要. (本书中用到的对象与元素同为一个事物)一个对象就像是一个人,他会有各种的特征(属性) , ...

  2. webdriver(python)学习笔记五——层级定位

    层级定位 在实际的项目测试中,经常会有这样的需求:页面上有很多个属性基本相同的元素,现在需要具体定位到其中的一个.由于属性基本相当,所以在定位的时候会有些麻烦,这时候就需要用到层级定位.先定位父元素, ...

  3. 转:python webdriver API 之操作测试对象

    一般来说,所有有趣的操作与页面交互都将通过 WebElement 接口,包括上一节中介绍的对象定位,以及本节中需要介绍的常对象操作.webdriver 中比较常用的操作元素的方法有下面几个: cle ...

  4. selenium python (七)层级定位(二次定位)

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #在实际测试过程中,一个页面可能有多个属性基本相同的元素,如果要定位到其 ...

  5. 转:python webdriver API 之对话框处理

    页面上弹出的对话框是自动化测试经常会遇到的一个问题:很多情况下对话框是一个 iframe,如上一节中介绍的例子,处理起来稍微有点麻烦:但现在很多前端框架的对话框是 div 形式的,这就让我们的处理变得 ...

  6. 转:python webdriver API 之定位一组对象

    webdriver 可以很方便的使用 find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法 find_eleme ...

  7. 转:python webdriver API 之定位 frame 中的对象

    在 web 应用中经常会出现 frame 嵌套的应用,假设页面上有 A.B 两个 frame,其中 B 在 A 内,那么定位 B 中的内容则需要先到 A,然后再到 B.switch_to_frame  ...

  8. 转:python webdriver API 之调用 JavaScript

    当 webdriver 遇到没法完成的操作时,笔者可以考虑借用 JavaScript 来完成,比下下面的例子,通过 JavaScript 来隐藏页面上的元素.除了完成 webdriver 无法完成的操 ...

  9. Webdriver API之元素定位

    Webdriver提供了8种元素定位方法:id.name.class name.tag name.link text.partial link text.xpath.css selector 一.以上 ...

随机推荐

  1. Decomposing and Redistributing the Statement Method

    Refactoring: Improving the Design of Existing Code Decomposing and Redistributing the Statement Meth ...

  2. StartSSL免费证书申请笔记

    第一步:申请startssl账号 填写相应信息后,你所填写的邮箱会收到邮件 里面有一个用来验证的验证码 输入得到的.... 注册成功后会安装数字证书(注意:注册过程中没有叫输入账号密码,这也是通过证认 ...

  3. CC2540的使用入门

    目录 1. 介绍 2. 开发环境 3. SDCC 1. 介绍 CC2540是一款2.4GHz Bluetooth® low energy SOC,基于8051 MCU 首先,你需要硬件设备 笔者的开发 ...

  4. HBase的属性

    一:基本属性 1.查看属性 2.解释属性 NAME:列簇名 BLOOMFILTER:布隆过滤器,用于对storefile的过滤 共有三种类型: ROW:行健过滤 ROWCOL:行列过滤 NONE:无 ...

  5. RTSP协议详解

        RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议.RTSP对流媒体提 ...

  6. [LeetCode]题解(python):036-Valid Sudoku

    题目来源 https://leetcode.com/problems/valid-sudoku/ etermine if a Sudoku is valid, according to: Sudoku ...

  7. 关于前后台交互生成json区别

    如何返回[object{xx:{}}]这种数组型json在服务器端return $arr[]=m;像这种都可以产生[Object { 0="9", 1="8", ...

  8. ios UIPickerView 技巧集锦(包括循环滚动)

    摘自: http://blog.csdn.net/ipromiseu/article/details/7436521 http://www.cnblogs.com/dabaopku/archive/2 ...

  9. Using Change Management and Change Control Within a Project

    In any project, change is inevitable whether it comes from within the project or from external sourc ...

  10. JS判断浏览器是否安装flash插件

    1.直接判断是否有flash插件 var myFlash = (function(){ if(typeof window.ActiveXObject != "undefined") ...