【问题标题】:Loop not iterating with Selenium, for loop through list循环不使用 Selenium 迭代,for 循环遍历列表
【发布时间】: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


    【解决方案1】:
    # 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"))
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      相关资源
      最近更新 更多