【问题标题】:How to use the WebDriverWait() to access the value of the next element having the same class names in selenium python如何使用 WebDriverWait() 在 selenium python 中访问具有相同类名的下一个元素的值
【发布时间】:2021-02-11 20:15:38
【问题描述】:

我正在尝试从该特定站点访问 YearQuarter 字段的值。在 StackOverflow 的一位成员的帮助下,我能够实现 year 部分的代码,现在如果我想访问 quarter 部分,那么我该如何访问呢。

下面是目前的实现。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-error')
options.add_argument('--ignore-ssl-errors')

url = "https://lifeinsurance.adityabirlacapital.com/about-us/public-disclosure"
driver = webdriver.Chrome(executable_path='drivers/chromedriver.exe')

driver.get(url)

WebDriverWait(driver, 20).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "div.selectize-input.items.full.has-options.has-items"))
).click()

year_dropdown = WebDriverWait(driver, 20).until(
    EC.visibility_of_all_elements_located(
        (By.CSS_SELECTOR, "div.selectize-dropdown-content div.option")
    )
)

for year in year_dropdown:
    print(year.text)

欢迎任何提示和建议。

【问题讨论】:

    标签: python selenium selenium-webdriver xpath webdriverwait


    【解决方案1】:

    您的年度和季度结构相同。一个是selectPublicYear,另一个是selectPublicQuarter

    wait = WebDriverWait(driver, 20)
    
    # Years
    year_selector = ".selectize-control.selectPublicYear"
    year_option_selector = year_selector + " .option"
    
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, year_selector))).click()
    
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, year_option_selector)))
    years = driver.find_elements(By.CSS_SELECTOR, year_option_selector)
    for year in years:
        print(year.text)
    years[-1].click()
    
    # Quarters
    quarter_selector = ".selectize-control.selectPublicQuarter"
    quarter_option_selector = quarter_selector + " .option"
    
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, quarter_selector))).click()
    # Wait for option element to be clickable.
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, quarter_option_selector)))
    # Get all option elements
    quarters = driver.find_elements(By.CSS_SELECTOR, quarter_option_selector)
    for quarter in quarters:
        print(quarter.text)
    # Select last one
    quarters[-1].click()
    

    您可以添加按值选择下拉列表的方法:

    def select_dropdown(name, selector, value):
        print(f'Select "{name}" dropdown, value: "{value}"')
    
        # Selectors
        selector = ".selectize-control" + selector
        option_selector = selector + " .option"
    
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).click()
    
        # Wait for option element to be clickable
        wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, option_selector)))
    
        # Get all options elements
        option_elements = driver.find_elements(By.CSS_SELECTOR, option_selector)
        # Get text values from options
        options = [opt.text.strip() for opt in option_elements]
        print(f'Available values: {options}')
    
        # Check if options have value
        if value not in options:
            raise Exception(f'"{name}" dropdown does not have "{value}" value. Available values: {options}')
    
        option_elements[options.index(value)].click()
    

    调用方法:

    select_dropdown(name="Year", selector=".selectPublicYear", value="2020-2021")
    select_dropdown(name="Quarter", selector=".selectPublicQuarter", value="Q2: Sep, 2020")
    

    【讨论】:

    • 我遵循了您的实现,它适用于打印值,但是当尝试使用 quarter.click() 时,它会抛出 StaleElementReferenceException 但它不会在年份部分给出此错误。你能告诉我为什么会这样吗?我该如何解决它。
    • 您可以查看更新代码。成功入选年份和季度。
    • 当我尝试选择 Q1: Jun, 2020 时,它给出了同样的错误。
    • 我通过添加 10 秒的延迟解决了这个问题。顺便感谢大家的帮助
    【解决方案2】:

    提取文本,例如Q4:2021 年 3 月,从所有使用 Selenium<class="option"> 中,您必须为 visibility_of_all_elements_located() 诱导 WebDriverWait,您可以使用以下任一 Locator Strategies

    • 使用XPATH

      driver.get('https://lifeinsurance.adityabirlacapital.com/about-us/public-disclosure')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[text()='Quarter']//following::div[@class='selectize-input items full has-options has-items']"))).click()
      print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='selectize-dropdown-content']//div[contains(@class, 'option')]")))])
      
    • 控制台输出:

      ['All Quarters', 'Q4: Mar, 2021', 'Q3: Dec, 2020', 'Q2: Sep, 2020', 'Q1: Jun, 2020']
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2018-04-12
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 2019-11-26
      • 2020-09-20
      • 2015-08-06
      相关资源
      最近更新 更多