转:python webdriver API 之定位一组对象
webdriver 可以很方便的使用 find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一
组对象,WebElement 接口同样提供了定位一组元素的方法 find_elements。
定位一组对象一般用于以下场景:
批量操作对象,比如将页面上所有的 checkbox 都勾上
先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的
checkbox,然后选择最后一个。
checkbox.html
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Checkbox</title>
<script type="text/javascript" async="
" src="https://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" />
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</head>
<body>
<h3>checkbox</h3>
<div class="well">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="c1">checkbox1</label>
<div class="controls">
<input type="checkbox" id="c1" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c2">checkbox2</label>
<div class="controls">
<input type="checkbox" id="c2" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c3">checkbox3</label>
<div class="controls">
<input type="checkbox" id="c3" />
</div>
</div>
</form>
</div>
</body>
</html>
将这段代码保存复制到记事本中,将保存成 checkbox.html 文件。 (注意,这个页面需要和我们的自动
化脚本放在同一个目录下,否则下面的脚本将指定 checkbox.html 的所在目录)
通过浏览器打开 checkbox.html,将看到以下页面:
图 3.5
通过图 3.4 可以看到页面提供了三个复选框和两个单选按钮。下面通过脚本来单击勾选三个复选框。
# -*- coding: utf-8 -*-
from selenium import webdriver
import os
driver = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('checkbox.html')
driver.get(file_path)
# 选择页面上所有的 tag name 为 input 的元素
inputs = driver.find_elements_by_tag_name('input')
#然后从中过滤出 tpye 为 checkbox 的元素,单击勾选
for input in inputs:
if input.get_attribute('type') == 'checkbox':
input.click()
driver.quit()
import os
os.path.abspath()
os 模块为 python 语言标准库中的 os 模块包含普遍的操作系统功能。主要用于操作本地目录文件。
path.abspath()方法用于获取当前路径下的文件。另外脚本中还使用到 for 循环,对 inputs 获取的一组元素
进行循环,在 python 语言中循环变量(input)可以不用事先声明直接使用。
find_elements_by_xx(‘xx’)
find_elements 用于获取一组元素。
下面通过 css 方式来勾选一组元素,打印当所勾选元素的个数并对最后一个勾选的元素取消勾选。
#coding=utf-8
from selenium import webdriver
import os
driveriver = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('checkbox.html')
driver.get(file_path)
# 选择所有的 type 为 checkbox 的元素并单击勾选
checkboxes = driver.find_elements_by_css_selector('input[type=checkbox]')
for checkbox in checkboxes:
checkbox.click()
# 打印当前页面上 type 为 checkbox 的个数
print len(driver.find_elements_by_css_selector('input[type=checkbox]'))
# 把页面上最后1个 checkbox 的勾给去掉
driver.find_elements_by_css_selector('input[type=checkbox]').pop().click()
driver.quit()
len()
len 为 python 语言中的方法,用于返回一个对象的长度(或个数) 。
pop()
pop 也为 python 语言中提供的方法,用于删除指定们位置的元素,pop()为空默认选择最一个元素。
转:python webdriver API 之定位一组对象的更多相关文章
- selenium python (六)定位一组对象
checkbox源码: <html><head><meta http-equiv="content-type" content="text/ ...
- 转:python webdriver API 之定位 frame 中的对象
在 web 应用中经常会出现 frame 嵌套的应用,假设页面上有 A.B 两个 frame,其中 B 在 A 内,那么定位 B 中的内容则需要先到 A,然后再到 B.switch_to_frame ...
- Python脚本控制的WebDriver 常用操作 <九> 定位一组对象
下面将使用WebDriver来模拟操作定位一组对象的操作 测试用例场景 从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需 ...
- Python+Selenium学习--定位一组对象
场景 从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法. ...
- 《selenium2 python 自动化测试实战》(7)——定位一组对象
定位一组对象 定位一组对象——find_elements_by_...(),注意,这里是elements,复数.返回的结果是一个列表,我们取值的时候就要用列表取值的方式来获得自己想要的元素.需要注意的 ...
- 转:python webdriver API 之 获取对象的属性
获取测试对象的属性能够帮我们更好的进行对象的定位.比如页面上有很多标签为 input 元素,而我们需要定位其中 1 个有具有 data-node 属性不一样的元素.由于 webdriver 是不支持直 ...
- webdriver(python)学习笔记四——定位一组元素
webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法. 定位一组对象一般用于以下场景: ...
- selenium python 定位一组对象
为什么定位一组对象? 定位一组对象的思想 在定位一组对象的过程中我们如何实现?以前的都是通过具体的对象定位,那么定位一组我们就需要通过css来定位 在单个定位对象中使用的是find_elem ...
- 定位一组对象-checkbox 、radiobutton
webdriver 可以很方便的使用find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法find_element ...
随机推荐
- 泌尿系统 Excretory system
https://zh.wikipedia.org/wiki/泌尿系统 泌尿系統,有時也歸類於排泄系統(Excretory system)的一部分,負責尿液的產生.運送.儲存與排泄.人類的泌尿系統包括左 ...
- mysql和oracle 分页查询(转)
最近简单的对oracle,mysql,sqlserver2005的数据分页查询作了研究,把各自的查询的语句贴出来供大家学习..... (一). mysql的分页查询 mysql的分页查询是最简单的,借 ...
- php 分词 —— PHPAnalysis无组件分词系统
分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...
- Startssl 现在就启用 HTTPS,免费的!
为什么要使用HTTPS 主要是为了安全,虽然没有100%的安全,但是我们可以尽量提高安全级别,目前大型网站都已经使用HTTPS了 注册StartSSL 注册页面 选择国家 和 输入 邮箱 他们会通过 ...
- FW Docker为容器分配指定物理网段的静态IP
官方有关于网桥和IP配置的文档地址:https://docs.docker.com/articles/networking/ 1.宿主机(系统采用ubuntu-14.04.1-server-amd64 ...
- docker kubernetes--
http://kubernetes.io/docs/getting-started-guides/docker/ https://hub.docker.com/r/solsson/hyperkube- ...
- 答CsdnBlogger问-关于职业发展和团队管理问题
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 问1:关于职业发展以及团队管理?(正能同學_) 请问在二线城市的小公司里,普通Android开发者的 ...
- Qt 之 使用 https发送 HTTP请求(使用OPENSSL库)
一.简述 在使用Qt发送HTTP请求中一般使用的链接都是http://前缀,而有的服务器支持 https://前缀的链接,而Qt本身是支持https的,但是https访问需要用到SSL认证,而QT默认 ...
- PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744
原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated ...
- Microsoft Dynamics AX 2009 White Paper: Close Non-Financial Transfers
http://www.microsoft.com/en-us/download/confirmation.aspx?id=12174