# -*- coding: utf-8 -*-
from selenium import webdriver import time import os dr = webdriver.Chrome() file = os.path.abspath("c:\\Temp\\checkbox.html") #獲取文件路徑
dr.get(file) # 選擇所有的checkbox並全部勾上
checkboxes = dr.find_elements_by_css_selector('input[type=checkbox]') for checkbox in checkboxes: checkbox.click() time.sleep(1) dr.refresh() time.sleep(2) # 打印當前頁面上有多少個checkbox
print len(dr.find_elements_by_css_selector('input[type=checkbox]')) # 選擇頁面上所有的input,然后從中過濾出所有的checkbox並勾選之
inputs = dr.find_elements_by_tag_name('input') for input in inputs: if input.get_attribute('type') == 'checkbox': input.click() time.sleep(1) # 把頁面上最后1個checkbox的勾給去掉
dr.find_elements_by_css_selector('input[type=checkbox]').pop().click() time.sleep(1) dr.quit()