简体   繁体   中英

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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