selenium python (七)层级定位(二次定位)
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'zuoanvip'
#在实际测试过程中,一个页面可能有多个属性基本相同的元素,如果要定位到其中的一个,这时候需要用到层级定位。先定位到父元素,然后再通过父元素定位子孙元素
#导入包
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
import os
driver = webdriver.Firefox()
#checkbox.html 要和脚本文件放一个目录下,否则需要指定checkbox.html的路径
file_path = 'file:///'+os.path.abspath('level_locate.html')
driver.get(file_path)
#首先定位到Link1链接(弹出下拉列表)
driver.find_element_by_id('xx').click()
#在父元素下找到link为Action的子元素
sub_element = driver.find_element_by_id('xx').find_element_by_link_text('Another_action')
#将鼠标移动到子元素上
ActionChains(driver).move_to_element(sub_element).perform()
time.sleep()
======================================================================================
level_locate页面源代码:
<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>
selenium python (七)层级定位(二次定位)的更多相关文章
- [译]Selenium Python文档:二、初步开始
2.1.简单使用 如果已经安装好了Selenium Python,你就可以像下面这样编写Python代码来使用它了: from selenium import webdriver from selen ...
- selenium+python自动化95-弹出框死活定位不到
前言 部分小伙伴学了selenium的alert后,就不管啥弹出框都去用alert,这是行不通的,看到弹出框,先要确定是不是alert,是才能用,不是的话肯定不能用. 有些弹出框是div层,这种跟平常 ...
- selenium+python编写自动化脚本时,定位frame中对象操作
在web应用中经常会出现frame嵌套的应用,假设页面上有A,B两个frame,其中B在A内,那么定位B中的内容则需要先到A,再到B.switchTo().frame方法可以把当前定位的主题切换到fr ...
- selenium + python 环境配置 (二)之启动IE
安装好python.selenium工具后,下一步就是启动浏览器 1.启动IE浏览器 即Selenium 调用IEDriverServer打开IE浏览器 ,因此需下载对应的IEDriverServer ...
- 【虫师讲Selenium+Python】第三讲:操作测试对象
一.首先呢,选择一个编辑器,我们这里选择的是Sublime Text >Ctrl+B为运行当前脚本的快捷方式 二.编写代码 #coding==utf-8 from selenium import ...
- Selenium定位二 --多个元素定位方法 和层级定位方法
定位多个元素: findElements()方法可以返回一个符合条件的元素List 组 如: public void hitUpdatePersonnel(WebDriver driver, int ...
- python+selenium二:定位方式
# 八种单数定位方式:elementfrom selenium import webdriverimport time driver = webdriver.Firefox()time.sleep(2 ...
- selenium+python自动化之xpath定位
在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. 什么是 ...
- Selenium+Python自动化测试实战(2)元素定位
1.Selenium2 的原理 第一篇分享简单介绍了基于Python开发的Selenium2的环境配置,这篇主要讲一下基本用法.首先讲一下Selenium2的基本原理.基本上知道了这个东西是怎么回事, ...
- selenium python 一些操作和定位收集
(—)滚动条操作 python中selenium操作下拉滚动条方法汇总 selenium_webdriver(python)控制浏览器滚动条 selenium+Python(select定位) Sel ...
随机推荐
- JSP文件下载及出现getOutputStream() has already been called for this response的解决方法
JSP文件下载及出现getOutputStream() has already been called for this response的解决方法 http://iamin.blogdriver.c ...
- Oracle ->> Oracle下生成序列的方法
用hierachical query,即connect by配合dual表生成序列,mod这个是取余函数,生成group factor.最后面的connect by rownum <= 100可 ...
- lua标签解析器
lua 标签解析器 概述 一个类xml标签解析函数,将标签解析成Lua中的表结构它可以用来解析简单xml结构,可以作为RichLabel控件的字符串解析组件(其实它现在就是这么用的;-)) 原理 使用 ...
- ActionScript 设置元件色彩属性
var clr:Color = new Color(mc); var ct:Object = {ra:100, rb:0, ga:100, gb:0, ba:100, bb:0, aa ...
- 【verilog】fdisplay中如何保存有符号形式
2014-01-02 10:10:29 参考:http://xilinx.eetop.cn/viewthread-275584 使用系统任务$signed,如 $fdisplay(fp, " ...
- Codeforces Round #206 div1 C
CF的专业题解 : The problem was to find greatest d, such that ai ≥ d, ai mod d ≤ k holds for each i. Let ...
- JS里面匿名函数的调用 & 变量作用域的实验
参考 http://www.educity.cn/wenda/54753.html 已实验验证结果正确. 1.下列哪些正确?(B.C) A.function(){ alert("Here!& ...
- gitflow workflow
https://www.atlassian.com/git/tutorials/comparing-workflows/forking-workflow Dream big, work smart, ...
- POJ3592 Instantaneous Transference tarjan +spfa
链接:http://poj.org/problem?id=3592 题意:题目大意:给定一个矩阵,西南角为出发点,每个单位都有一订价值的金矿(#默示岩石,不成达,*默示时佛门,可以达到指定单位),队# ...
- 原创:无错版!让DEDE只生成一个RSS文件,不分栏目
DEDE为每一个栏目都独立创建一个rss文件, 如果用户要整站订阅相当不方便. 所以需要修改让dede只生成一个rss. 网上大部分帖子要么是抄, 要么是有问题少了步骤. 今天特意整理下. 分享.. ...