checkbox
一、利用tag_name定位一组复选框
from selenium import webdriver
import os,time
driver=webdriver.Firefox()
file_path='file:///'+os.path.abspath('checkbox.html')
driver.get(file_path)
#选择页面上所有的tag_name为input元素
inputs=drivers.find_elements_by_tag_name('input')
#然后从中过滤出type为checkbox的元素,点击勾选
for i in inputs:
if i.get_attribute('type')=='checkbox':
i.click()
time.sleep(1)
driver.quit()
二、通过xpah或css定位
a.通过xpath
checkboxs=driver.find_elements_by_xpath("//input@[type='checkbox']")
b.通过css
checkboxs=driver.find_elements_by_css_selector('input[type="checkbox"]')
for checkbox in checkboxs:
checkbox.click()
time.sleep(1)
#打印页面上type类型为checkbox的个数
print(len(checkboxs))
#把页面上最后一个checkbox的钩给去掉
driver.find_elements_by_css_selector('input[type=""checkbox]').pop().click()
driver.quit()
pop()或pop(-1)默认获取一组元素中的最后一个
pop(0)默认获取一组元素中的第一个
浙公网安备 33010602011771号