通过定位一组元素的方法来,来勾选自己需要勾选的选项。
1 from selenium import webdriver 2 from selenium.common.exceptions import NoSuchElementException 3 from time import sleep 4 5 6 driver = webdriver.Firefox() 7 driver.implicitly_wait(10) 8 9 url = "http://192.168.117.9:8080/jforum/forums/list.page" 10 driver.get(url) 11 try: 12 #登录 13 driver.find_element_by_name("username").send_keys("admin") 14 driver.find_element_by_name("password").send_keys("admin") 15 driver.find_element_by_name("login").click() 16 17 test_forum = driver.find_element_by_css_selector("[href='/jforum/forums/show/1.page']") 18 print(test_forum.text) 19 test_forum.click() 20 21 driver.find_element_by_xpath("//a[@class='icon_new_topic']/img").click() 22 23 """定位一组元素""" 24 all = driver.find_elements_by_css_selector("input[type='checkbox']") 25 num = 0 26 for checkbox in all: 27 num += 1 28 if num == 2 or num == 3: 29 checkbox.click() 30 sleep(1) 31 print('循环次数:',num) 32 '''取消选择的√''' 33 all.pop(-1).click() 34 35 except NoSuchElementException as e: 36 print(e) 37 38 finally: 39 sleep(10) 40 driver.quit()