selenium_webdriver(python)单/复选框的遍历选择
2015-04-30 16:36
363 查看
wml.html:
#coding: utf-8
#以下代码用来遍历所有复选框
from selenium import webdriver
import time
import os
driver = webdriver.Chrome()
#获取要测试文件绝对路径
file_path = os.path.abspath('wml.html')
print file_path
#用浏览器打开文件
driver.get(file_path)
# 选择页面上所有的input,然后从中过滤出所有的checkbox 并勾选之
inputs = driver.find_elements_by_tag_name('input')
for input in inputs:
if input.get_attribute('type') == 'checkbox':
input.click()
time.sleep(2)
'''
checkboxs = driver.find_elements_by_css_selector('input[type=checkbox]')
for checkbox in checkboxs:
checkbox.click()
time.sleep(2)
'''
# 把页面上最后1个checkbox 的勾给去掉
#.pop() 方法用于删除并返回数组的最后一个元素,在此处是返回到复选框的“最后一个”按钮
driver.find_elements_by_css_selector('input[type=checkbox]').pop().click()
time.sleep(2)
driver.quit()
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>Checkbox</title> </head> <body> <h3>checkbox</h3> <form> <!-- <label for="c1">checkbox1</label> --> <input type="checkbox" id="c1" />checkbox1<br> <!-- <label for="c2">checkbox2</label> --> <input type="checkbox" id="c2" />checkbox2<br> <!-- <label for="c3">checkbox3</label> --> <input type="checkbox" id="c3" />checkbox3<br> </form> <form> <label value="radio">radio</label> <input type="radio" name="sex" value="male" id="as"/><br> <label value="radio1">radio</label> <input type="radio" name="sex" value="female" id="sd"/> </form> <!-- <form> <input type="radio" name="sex" value="male" /> Male <br /> <input type="radio" name="sex" value="female" /> Female </form> --> </body> </html>
#coding: utf-8
#以下代码用来遍历所有复选框
from selenium import webdriver
import time
import os
driver = webdriver.Chrome()
#获取要测试文件绝对路径
file_path = os.path.abspath('wml.html')
print file_path
#用浏览器打开文件
driver.get(file_path)
# 选择页面上所有的input,然后从中过滤出所有的checkbox 并勾选之
inputs = driver.find_elements_by_tag_name('input')
for input in inputs:
if input.get_attribute('type') == 'checkbox':
input.click()
time.sleep(2)
'''
checkboxs = driver.find_elements_by_css_selector('input[type=checkbox]')
for checkbox in checkboxs:
checkbox.click()
time.sleep(2)
'''
# 把页面上最后1个checkbox 的勾给去掉
#.pop() 方法用于删除并返回数组的最后一个元素,在此处是返回到复选框的“最后一个”按钮
driver.find_elements_by_css_selector('input[type=checkbox]').pop().click()
time.sleep(2)
driver.quit()
#coding: utf-8 ##以下代码用来遍历所有单选框 from selenium import webdriver import time import os dr = webdriver.Chrome() file_path = os.path.abspath('wml.html') dr.get(file_path) # 选择所有的radio并全部勾上 radios = dr.find_elements_by_css_selector('input[type=radio]') for radio in radios: radio.click() time.sleep(2) time.sleep(2) dr.quit()
相关文章推荐
- Selenium-Webdriver(python) (十) --- 下拉框的选择
- 【selenium+Python WebDriver API】之复选框顺序正选和顺序反选
- selenium_webdriver(python)查看文件路径,鼠标定位
- 轻松自动化---selenium-webdriver(python) (六)
- windows下python selenium+webdriver环境配置
- Python+Selenium(webdriver常用API)
- selenium_webdriver(python)下拉列表、下拉框和js弹窗警告处理
- 译:selenium webdriver (python)
- python selenium webdriver处理浏览器滚动条
- selenium_webdriver(python)键盘组合键用法,输入中文问题
- Fixing “selenium.common.exceptions.WebDriverException: Message: ‘Can\’t load the profile.” 分类: 问题总结 python下载 ubuntu python基础学习 2014-01-20 15:28
- 轻松自动化---selenium-webdriver(python) (三)
- selenium-webdriver(python) (十五) -- 鼠标事件
- Selenium自动化测试Python五:WebDriver设计模式
- selenium+webdriver爬取动态网页介绍_python
- pycharm + selenium + python 提示 Unresolved reference 'webdriver' 解决办法
- selenium-webdriver(python) (十) 如何处理下拉框
- Selenium自动化测试Python三:WebDriver进阶
- python selenium-webdriver 元素操作之鼠标操作(四)
- webdriver cookie 处理 selenium_python