简体   繁体   English

循环不使用 Selenium 迭代,for 循环列表

[英]Loop not iterating with Selenium, for loop through list

on website https://icem.data-archive.ac.uk/#step1 , I am trying to loop over the years with the follwing loop setup (full code at end of question):在网站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 

My problem is that it doesn't select the next year in the years list, although it does select the first.我的问题是,它不是 select 明年在年份列表中,尽管它确实是 select 第一个。 Anthing I'm missing for the loop to keep on going?为了让循环继续下去,我缺少什么?

Thank you谢谢

Full code, for any purpose:完整代码,用于任何目的:

## 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 
# 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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM