Selenium – Unable to find element

Question:

I have problems clicking a drop-down menu. The problem occurs in the second line from the bottom. The error i get is that selenium is unable to find the element i would like to click. I’ve tried with Xpath and ID instead of Class name, but that doesn’t work neither. All code until that point works fine, so i can’t figure out what the problem is.

The element i would like to click is highlighted in the attached picture.
[1]: https://i.stack.imgur.com/X4bFX.png

    # Ă…ben chrome
    driver = webdriver.Chrome(path)
    driver.get(web)

    # Accept cookies
    accept = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="onetrust-accept-btn-handler"]')))
    accept.click()

    header = driver.find_element(By.CLASS_NAME, 'updated-competitions')

    turneringer = WebDriverWait(header, 5).until(EC.element_to_be_clickable((By.XPATH, './/a[contains(@title, "COMPETITIONS")]')))
    turneringer.click()

    turneringer_tabel = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, 'mod-multipickazmenu-1054')))
    sti = './/div[contains(@data-category, "' + country + '")]'
    land_knap = WebDriverWait(turneringer_tabel, 5).until(EC.element_to_be_clickable((By.XPATH, sti)))
    land_knap.click()

    sti = './/a[contains(@data-galabel, "' + dict_countries[country][league] + '")]'
    liga_knap = WebDriverWait(turneringer_tabel, 5).until(EC.element_to_be_clickable((By.XPATH, sti)))
    liga_knap.click()

    dict_odds = {}

    for i, market in enumerate(markets):
        dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'marketchooser-container new-coupon-layout')))
        dropdown.click()
Asked By: McCool

||

Answers:

@McCool, Instead of the following line:

  dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'marketchooser-container new-coupon-layout')))

replace it with:

  dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.marketchooser-container.new-coupon-layout')))

The reason is in Selenium CLASS_NAME does not support composite classes. In the current case, marketchooser-container and new-coupon-layout are separate classes. Hence using CSS_SELECTOR you can specify two classes as given above. If this does not solve your problem, then request you to share the stack trace of the error you are getting.

Answered By: ketanvj