定位一组对象-checkbox 、radiobutton
webdriver 可以很方便的使用find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法find_elements。
定位一组对象一般用于以下场景:
- 批量操作对象,比如将页面上所有的checkbox 都勾上。
- 先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的checkbox,然后选择最后一个。
checkbox.html 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>
补充后的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>
<div class="control-group">
<label class="control-label" for="r">radio</label>
<div class="controls">
<input type="radio" id="r1" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="r">radio</label>
<div class="controls">
<input type="radio" id="r2" />
</div>
</div>
</form>
</div>
</body>
</html>
将这段代码保存复制到记事本中,将保存成checkbox.html 文件。(注意,这个页面需要和我们的自动化脚本放在同一个目录下,否则下面的脚本将checkbox.html 的所在目录)
通过浏览器打开checkbox.html,将看到以下页面:

图3.5
如果3.5所示:
可以看到页面提供了三个复选框和两个单选按钮。下面通过脚本来单击勾选三个复选框。
代码示例:
# -*- 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()
你可以试着把input.get_attribute('type') == 'checkbox' 中的checkbox 变成radio ,那这个脚本定位的会是两个单选框
说明:
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()为空默认选择最一个元素。
尝试:
把find_elements_by_css_selector('input[type=checkbox]').pop().click() 中的checkbox 变成radio 会是什么效果,自己尝试一下吧!
知识扩充:
参考博客地址:http://www.cnblogs.com/yoyoketang/p/6128675.html
定位一组对象-checkbox 、radiobutton的更多相关文章
- 转:python webdriver API 之定位一组对象
webdriver 可以很方便的使用 find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法 find_eleme ...
- Python脚本控制的WebDriver 常用操作 <九> 定位一组对象
下面将使用WebDriver来模拟操作定位一组对象的操作 测试用例场景 从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需 ...
- selenium python (六)定位一组对象
checkbox源码: <html><head><meta http-equiv="content-type" content="text/ ...
- selenium python 定位一组对象
为什么定位一组对象? 定位一组对象的思想 在定位一组对象的过程中我们如何实现?以前的都是通过具体的对象定位,那么定位一组我们就需要通过css来定位 在单个定位对象中使用的是find_elem ...
- Python+Selenium学习--定位一组对象
场景 从上一节的例子中可以看出,webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法. ...
- 自动化测试-7.selenium定位一组对象
前言 前面的几篇都是讲如何定位一个元素,有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象. webdriver 提供了定位一组元素的方法,跟前面八种定位方式 ...
- 《selenium2 python 自动化测试实战》(7)——定位一组对象
定位一组对象 定位一组对象——find_elements_by_...(),注意,这里是elements,复数.返回的结果是一个列表,我们取值的时候就要用列表取值的方式来获得自己想要的元素.需要注意的 ...
- Python+Selenium 自动化实现实例-定位一组对象(checkbox,inputs)
# -*- coding: utf-8 -*- from selenium import webdriver import time import os dr = webdriver.Chrome() ...
- webdriver(python)学习笔记四——定位一组元素
webdriver可以很方便的使用find_element方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用find_elements方法. 定位一组对象一般用于以下场景: ...
随机推荐
- 数迹学——Asp.Net MVC4入门指南(4):添加一个模型
一.添加模型类 二.添加MovieDBContext类,连接数据库 DbContext类继承自 System.Data.Entity; 负责在数据库中获取,存储,更新,处理实例 MovieDBCont ...
- php 单引号 双引号 ,php字符串/ hmtl / 数据库显示/ 及php的几个转化函数
* 以单引号为定界符的php字符串,支持两个转义\'和\\* 以双引号为定界符的php字符串,支持下列转义(\'会直接输出\' ,也会转义 \\): \n 换行(LF 或 ASCII 字符 0x ...
- Android学习四:数据库操作
1前言 android中使用SQLite作为数据库,在进行相关的开发的时候不需要导入包.SQLite起符合SQL标准,也有自己的一些特性,是一个轻量级的数据库. 2代码 简单的数据库类封装 packa ...
- Python之paramiko基础
一.Paramiko模块 paramiko是一个自由和开放源码模块使用,实现SSH2协议安全(认证和加密)连接到远程计算机. 二.windwos下安装paramiko模块 #在DOS命令行执行如下命令 ...
- SQL Server DBA日常查询视图_数据库性能视图
1.获取有关按平均CPU 时间排在最前面的五个查询的信息 total_worker_time/execution_count AS [Avg CPU Time], ), ((CASE qs.state ...
- linux shell mysql 数据库主从同步状态检查告警
需求: 1.监测数据库主从状态 2.获取数据库主要参数 3.可读取配置文件 4.部署位置自适应. 参考资料: http://blog.csdn.net/yf210yf/article/detail ...
- 【python】进程
multiprocessing 如果你打算编写多进程的服务程序,Unix/Linux无疑是正确的选择.由于Windows没有fork调用,难道在Windows上无法用Python编写多进程的程序? 由 ...
- python 简单爬虫diy
简单爬虫直接diy, 复杂的用scrapy import urllib2 import re from bs4 import BeautifulSoap req = urllib2.Request(u ...
- python 数据库 blob类型 转字符串
例如: 从数据库里读出了blob类型,如 z = b'61736467' 在py里转化成字符串:bytes.fromhex(z).decode('utf8')
- .NET使用一般处理程序生成验证码
运行的效果图: HTML的代码: <head> <script type="text/javascript"> function changeCode() ...