天天看點

Selenium3+python自動化009- 多選框

多選框

# 随機選擇多選框

# sports=driver.find_elements_by_name("sport")

# maxnum=len(sports)

# num=random.randint(0,maxnum-1)

# sports[num].click()

#encoding=utf-8
from selenium import webdriver
from time import sleep
import os

driver=webdriver.Chrome()
url='file:///' + os.path.abspath(os.path.dirname(os.getcwd()))+'/html/注冊A.html'

driver.maximize_window()
driver.get(url)

scroll="document.documentElement.scrollTop=800"#垂直滾動 px
driver.execute_script(scroll)

carCheckBox=driver.find_element_by_id('qcA')
carCheckBox.click()
sleep(2)
if carCheckBox.is_selected():
    carCheckBox.click()

checkBoxList=driver.find_elements_by_xpath('//input[@name="checkbox"]')
for box in checkBoxList:
    if not box.is_selected():
        box.click()