循环不迭代Selenium, for循环遍历list



在网站https://icem.data-archive.ac.uk/#step1上,我试图用以下循环设置循环多年(问题结束时的完整代码):

yearslist = webD.find_elements_by_xpath('//input[@type="radio"]')

for elem in yearslist:
elem.click()
...

WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/section/section[4]/article/button"))).click()
# This is a restart the process button, sending me back to years selection page

continue 

我的问题是它没有在年份列表中选择下一年,尽管它确实选择了第一年。我还缺什么让循环继续下去吗?

谢谢

完整代码,用于任何用途:

## SELENIUM SETUPfrom selenium.webdriver.support.ui import WebDriverWait
import selenium
import time
from selenium import webdriver as wd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

chrome_path = r'C:webdriverschromedriver.exe'
webD=wd.Chrome(executable_path=chrome_path)
# LOGIN
webD.get('https://beta.ukdataservice.ac.uk/myaccount')
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="myaccount-login"]/div/div/div/div/div/div[2]/div/div/div/form/div/p/button'))).click()

WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="username"]'))).send_keys('ukd1217078879')
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//*[@id="password"]'))).send_keys('Census4Me!')
webD.find_element_by_xpath('/html/body/div/div/div[2]/div[1]/form/div[6]/button').click()

# CENSUS FORM 
webD.get('https://icem.data-archive.ac.uk/#step1')

## STEP 1: SELECTING A YEAR, HERE 1851
yearslist = webD.find_elements_by_xpath('//input[@type="radio"]')
#listofyears = webD.find_elements_by_css_selector("#input li")

for elem in yearslist:
elem.click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//b[@id = "country_england"]/preceding-sibling::input'))).click()
webD.find_element_by_xpath('//html/body/div/section/section[1]/article[2]/div/div/button').click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="county_category"]/button[1]'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="county_category"]/div[2]/div/div[2]/button'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div/div[3]/div[1]/label/input'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Apply')]"))).click()
WebDriverWait(webD, 20).until(EC.presence_of_element_located((By.XPATH, '//span[@class = "ice-allow-download-alert ng-animate ng-hide-remove ng-hide-remove-active"]')))
WebDriverWait(webD, 20).until(EC.invisibility_of_element_located((By.XPATH, '//span[@class = "ice-allow-download-alert ng-animate ng-hide-remove ng-hide-remove-active"]')))
WebDriverWait(webD, 20).until(EC.presence_of_element_located((By.XPATH, '//b[text()[contains(.,"HISCO classified occupation")]]/..'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="workhistoryunit_category"]/div[2]/div/div[2]/button'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div/div/div[3]/div[1]/label/input'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Apply')]"))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/section/section[2]/article[1]/div/article/div[4]/button"))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/section/section[3]/label[1]/input"))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/section/section[3]/button[2]"))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/section/section[4]/article/button"))).click()
continue 
# 1st approach through <div> "ng-show" attribute
for div in driver.find_elements_by_tag_name("div"):
if div.get_attribute("ng-show") == "years":
all_years = [b.text for b in div.find_elements_by_tag_name("b")]
break
# 2nd approach thought <input> "type" attribute
all_years_2 = []
for input_tag in driver.find_elements_by_tag_name("input"):
if input_tag.get_attribute("type") == "radio":
all_years_2.append(input_tag.get_attribute("value"))

最新更新