How to use the WebDriverWait() to access the value of the next element having the same class names in selenium python

Tags: , , , ,

I am trying to access the values of both the fields Year and Quarter from this particular site. With the help from one member of StackOverflow, I was able to implement the code for the year part, now if I want to access the quarter part so how can I access the same.

Below is the implementation so far.

from selenium import webdriver
from import WebDriverWait
from import By
from import expected_conditions as EC

options = webdriver.ChromeOptions()

url = ""
driver = webdriver.Chrome(executable_path='drivers/chromedriver.exe')


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

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

for year in year_dropdown:

Any hints and suggestions are welcome.


You have same structure for both years and quarters. One with selectPublicYear and another with selectPublicQuarter class

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:

# 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:
# Select last one

You can add method to select dropdown by value:

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}')


Call method:

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

Source: stackoverflow