How to select a dropdown menu using Selenium Python


I’m working on a personal project, trying to use selenium to web scrape my locals teams results from a website. The site has three drop-down lists, at the moment I’d be happy to just be able to alter one. The code below clicks reject cookies when I enter the page, that’s what the WebDriver line does. I’m getting as far as ‘dropdown’ and then an error that no such element exists. I’ve tried CSS_SELECTOR, XPATH and all the other options to no avail. I am only new to this so it could be something simple I’m missing but I’ve read a lot of the previous forums with similar questions and the answers didn’t work for me.

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

driver = webdriver.Firefox()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='onetrust-reject-all-handler']"))).click()


driver.quit() # close browser


The element with dropdown options as Men, Schools, Women, Youth is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='']")))
      Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[name=user]")))).select_by_value('7183')
    • Using XPATH:

      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='']")))
      Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@name='user']")))).select_by_value('7183')
  • Note : You have to add the following imports :

    from import WebDriverWait
    from import By
    from import expected_conditions as EC
  • Browser Snapshot:


Answered By – undetected Selenium

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published