文件名:checkbox.html
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <title>Checkbox</title> <link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/> <script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/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>
文件名:test10.py
from selenium import webdriver import os,time driver = webdriver.Chrome() file_path=‘file:///‘+os.path.abspath(‘checkbox.html‘) driver.get(file_path) inputs = driver.find_elements_by_tag_name(‘input‘) for i in inputs: if i.get_attribute(‘type‘)==‘checkbox‘: i.click() time.sleep(1) driver.quit()
test10.py代码中的意思:访问checkbox.html
通过tag方法访问checkbox.html中所有的input元素
找到input元素之后,通过判断input属性中的"chekcbox"属性,从而定位出需要定位的一组元素
原文地址:http://9603083.blog.51cto.com/9593083/1870495