Selenium-测试对象操作之:复选框checkbox
复选框操作包括:选中、取消选中、全选
案例:
Python+Selenium代码
# -*- coding: utf-8 -*-
from selenium import webdriver
import os
import time
file_path = os.path.abspath('checkbox.html')
print file_path
driver = webdriver.Chrome()
driver.get(file_path)
#选中一个复选框
driver.find_element_by_id("c1").click()
#打印该复选框的选中状态
print driver.find_element_by_id("c1").is_selected()
time.sleep(3)
#取消选中
driver.find_element_by_id("c1").click()
print driver.find_element_by_id("c1").is_selected()
# 全选:选择页面上所有的 tag name 为 input 的元素
inputs = driver.find_elements_by_tag_name('input')
#然后从中过滤出 tpye 为 checkbox 的元素,单击勾选
for inp in inputs:
if inp.get_attribute('type') == 'checkbox':
inp.click()
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>
<input type="text" id="in1" value="干扰输入框" />
</form>
</div>
</body>
</html>