码迷,mamicode.com
首页 > 其他好文 > 详细

Selenium-测试对象操作之:复选框checkbox

时间:2017-03-25 17:37:38      阅读:2004      评论:0      收藏:0      [点我收藏+]

标签:check   char   取消   css   jquer   UI   form   nbsp   google   

复选框操作包括:选中、取消选中、全选

 

案例:

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>

 

Selenium-测试对象操作之:复选框checkbox

标签:check   char   取消   css   jquer   UI   form   nbsp   google   

原文地址:http://www.cnblogs.com/yan-xiang/p/6617633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!