【发布时间】:2021-02-04 13:33:11
【问题描述】:
在网站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:\webdrivers\chromedriver.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
【问题讨论】:
标签: python selenium loops for-loop selenium-webdriver