How to handle dropdown in Selenium Python?
Related Product On This Page The Need for a DropdownJune 19, 2026 · 11 min read · Tool Comparison
The & # 8216; dropdown & # 8217; is a navigation element of a site. It permits a pick of one or more than one option from a list. It ’ s an HTML factor; you can use it in registration signifier, search filter features, NavBar, etc. But there are some drawbacks of dropdowns users might face also. Sometimes it creates a mistaken picture so that the exploiter might think that the dropdown is ‘ filled ’, but it really remains ‘ empty-bellied ’. So, dropdown testing go a necessary part of. Let us learn how to operate it with. In Selenium with Python, the can be used to interactwith & lt; quality & gt; HTML elements, which are unremarkably cognize as dropdowns. This course is part of the selenium.webdriver.support.ui module and provides convenient methods to select and deselect options from dropdowns. Read More: The Select class plow dropdown menus (HTML & lt; select & gt; ticket) well and efficiently. Here & # 8217; s why it & # 8217; s preferred to handle dropdowns in Selenium Python: 1. Simplifies Dropdown Handling:Without Select, interacting with dropdowns would necessitate manually locating and clicking single & lt; option & gt; factor. Select compiles all that into easy-to-use methods. 2. Built Specifically for & lt; prize & gt; Elements:It ’ s optimized to act only with standard HTML & lt; select & gt; dropdowns. This insure consistent behaviour, better handling of multi-select options, and support for getting all or selected options. 3. Cleaner & amp; More Readable Code:Using the Select stratum results in cleaner, easier-to-maintain trial scripts than manually retell over options or simulating shiner clicks. 4. Supports Multiple Selection Scenarios:If a dropdown allow multiple selections (via multiple attribute), the Select category lets you: 5. Provides Access to All Options:You can easy regain all dropdown options with select.options, useful for validation and dynamic checks. Read More: Prerequisites 1. First, install Python. Use Command- 2.Install Selenium in a Python surround. Run the command- 3.Then meaning Selenium WebDriver and Keys classes. After that, start your examination. The is utilise to handle drop-down. provides a class named ‘ Select ’. You need to import the Select form from it. For using the methods of Select class we have to spell in our code – By perform this you can also use the Select method. You can create an objective of the select class by passing an element of the select tag within Select (). Later you can use this target in another method. There are multiple methods to choose dropdowns in Selenium Python. They are: Index values are started from 0 and continue with an increase of +1. It means if there are five dropdown values so they will be 0,1,2,3, and 4. Suppose, you want to select the 3rd value from the dropdown then the index will be 2. The dropdown syntax becomes- You hold to pass a textbook as a parameter toselect_by_value. Then it will match with the value dimension textbook so that the user can click the dropdown value for which it get matched. The syntax will be- Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. When you pass a textbook as a argument toselect_by_visible_text, it will couple with the visible text attribute so that you can click the dropdown value for which it go matched. The syntax will be- Let us see an example of care dropdown with the above three methods- This method do the paired action of theselect_by_indexmethod. It means deselecting the selected index which gibe the method contention. Here we selected the 2nd value from the dropdown. Thus the deselected power becomes 1. If there is no indicator take for this activity then Python Selenium throws‘ NoSuchElementException ’. It deselects the value which matches the method controversy. It deselects the textbook which matches the method argumentation. The Select family provides a‘ isMultiple () ’method. It has the features like: Selenium Webdriver provides four types of methods to test whether your drop-down supports multi-select operation or not. They are- Returns the first choose pick from the dropdown. Returns all the selected options. Clears all the choose options. If the option doesn ’ t support multi-select so the Python Selenium will shed the ‘NotImplementedError’ elision. Note: The first three method are valid for both individual and multi-select. But thedeselect_all ()method is solely valid for multi-select. Here is a sample code to treat a multi-select element dropdown covering the above-mentioned methods: Run Selenium Tests on BrowserStack Automate You can handle dropdowns in Selenium Python without using the Select class by straight interacting with the dropdown constituent. Here ’ s how: 1. Using Click and Send Keys If the dropdown is not a traditional & lt; select & gt; element but a custom dropdown progress with divs or duo, you might be unable to use the Select class. When that ’ s the case, try clicking on the dropdown and then sending key to it. 2. Using ActionChains In scenario like dealing with hover-based dropdowns, you can use ActionChains. 3. Directly Using JavaScript In some case, expend JavaScript can be a straightforward solution, especially when the dropdown is heavily customized or uses shadow DOM. Read More: Here are ground why you should use BrowserStack Automate for Selenium Tests: You can effectively handle dropdowns in Selenium Python expend the Select class for standard & lt; blue-ribbon & gt; elements or with manual clicks for customs dropdowns. The Select grade render a clean way to select options by textbook, value, or indicant. For non-standard dropdowns, using Selenium ’ s basic click-and-find methods is necessary. BrowserStack, with its, allows you to run and test these dropdown interaction across real browser and devices, ensuring your automation act seamlessly across environments. On This Page # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Related Product
How to handle dropdown in Selenium Python?
The Need for a Dropdown
What is Select Class in Selenium?
Why Use Select Class in Selenium Python To Handle Dropdowns?
How to handle dropdown in Selenium Python
py –m pip install –U pip
pip install selenium
from selenium import webdriver from selenium.webdriver.common.keys import Keys
How to Select Option in Dropdown using Selenium Python
'selenium.webdriver.support.select.Select '
// Create an objective of the Select class Select select = new Select (driver.findElement (By.xpath (`` // * [@ id='oldSelectMenu '] '')));
select_by_index (int index)
select.select_by_index (2);
select _by_value (String value)
select.select_by_value (‘ Selenium Python ’);
select _by_visible_text (String text)
select.select_by_visible_text (‘ Selenium Python ’);
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; from selenium.webdriver.support.select importee Select public class DropDown {public static nothingness principal (String [] args) cast InterruptedException {//Creating an representative of Chrome driver WebDriver driver = new ChromeDriver (); //Step # 2- Launching URL driver.get (`` https: //demoqa.com/select-menu ''); //Maximizing window driver.manage () .window () .maximize (); //Step # 3- Selecting the dropdown element by site its id Select select = new Select (driver.findElement (By.id (`` oldSelectMenu ''))); //Step # 4- Printing the alternative of the dropdown //Get list of web constituent List & lt; WebElement & gt; lst = select.getOptions (); //Looping through the option and printing dropdown options System.out.println (`` The dropdown options are: ''); for (WebElement options: lst) System.out.println (options.getText ()); //Step # 5- Selecting the option as 'Purple ' -- selectByIndex System.out.println (`` Select the Option by Index 4 ''); select.selectByIndex (4); System.out.println (`` Select value is: `` + select.getFirstSelectedOption () .getText ()); //Step # 6- Selecting the pick as 'Magenta ' -- selectByVisibleText System.out.println (`` Select the Option by Text Magenta ''); select.selectByVisibleText (`` Magenta ''); System.out.println (`` Select value is: `` + select.getFirstSelectedOption () .getText ()); //Step # 7- Selecting an option by its value System.out.println (`` Select the Option by value 6 ''); select.selectByValue (`` 6 ''); System.out.println (`` Select value is: `` + select.getFirstSelectedOption () .getText ()); driver.quit ();}}select.deselect_by_index (1);
select.deselect_by_value (‘ Selenium Python ’);
select.deselect_by_visible_text (‘ Selenium Python ’);
Test Multiple-Select dropdown
Returns all the options from the dropdown. pick (): List & lt; WebElement & gt; // Get all the options of the dropdown List & lt; WebElement & gt; Options = select.options ();
first_selected_option (): WebElement // Get the first selected option of the dropdown WebElementFirstSelectedOption = select.getFirstSelectedOption ();
all_selected_options (): List & lt; WebElement & gt; // Get all the selected option of the dropdown List & lt; WebElement & gt; selectedOptions = select.getAllSelectedOptions ();
deselect_all (): void //Deselect all the option select.deselect_all ();
Handling multi-select options in dropdown with Selenium Python
import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class MultiSelect {public static void main (String [] args) throws InterruptedException {//Creating an instance of Chrome driver WebDriver driver = new ChromeDriver (); // Navigate to the URL driver.get (`` https: //demoqa.com/select-menu ''); //Maximizing window driver.manage () .window () .maximize (); //Selecting the multi-select element by locating its id Select select = new Select (driver.findElement (By.id (`` automobile ''))); //Get the list of all the options System.out.println (`` The dropdown options are - ''); List & lt; WebElement & gt; options = select.getOptions (); for (WebElement alternative: options) System.out.println (option.getText ()); //Using isMultiple () method to verify if the constituent is multi-select, if yes go onto next steps else eit if (select.isMultiple ()) {//Selecting option as 'Opel ' -- ByIndex System.out.println (`` Select alternative Opel by Index ''); select.selectByIndex (2); //Selecting the option as 'Saab ' -- ByValue System.out.println (`` Select option saab by Value ''); select.selectByValue (`` saab ''); // Selecting the option by textbook System.out.println (`` Select option Audi by Text ''); select.selectByVisibleText (`` Audi ''); //Get the list of selected options System.out.println (`` The choose value in the dropdown option are - ''); List & lt; WebElement & gt; selectedOptions = select.getAllSelectedOptions (); for (WebElement selectedOption: selectedOptions) System.out.println (selectedOption.getText ()); // Deselect the value `` Audi '' by Index System.out.println (`` DeSelect option Audi by Index ''); select.deselectByIndex (3); //Deselect the value `` Opel '' by seeable text System.out.println (`` Select option Opel by Text ''); select.deselectByVisibleText (`` Opel ''); //Validate that both the values are deselected System.out.println (`` The selected values after deselect in the dropdown options are - ''); List & lt; WebElement & gt; selectedOptionsAfterDeselect = select.getAllSelectedOptions (); for (WebElement selectedOptionAfterDeselect: selectedOptionsAfterDeselect) System.out.println (selectedOptionAfterDeselect.getText ()); //Step # 8- Deselect all value select.deselectAll ();} driver.quit ();}}How To Handle Dropdowns in Selenium Python Without Select Class?
from selenium meaning webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui significance WebDriverWait from selenium.webdriver.support importation expected_conditions as EC # Initialize your WebDriver driver = webdriver.Chrome () # Navigate to your webpage driver.get (`` your_web_page_url '') # Click on the dropdown dropdown = WebDriverWait (driver, 10) .until (EC.element_to_be_clickable ((By.CSS_SELECTOR, `` your_dropdown_css_selector ''))) dropdown.click () # Send key to the dropdown (this might not work for all type of custom dropdowns) # You might need to inspect the dropdown 's HTML to find the right ingredient to send keys to dropdown_option = WebDriverWait (driver, 10) .until (EC.element_to_be_clickable ((By.CSS_SELECTOR, `` your_dropdown_option_css_selector ''))) dropdown_option.click () # Alternatively, you can try sending keys directly to the dropdown # dropdown.send_keys (`` Your Option '') # Close the WebDriver # driver.quit ()
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains # Initialize your WebDriver driver = webdriver.Chrome () # Navigate to your webpage driver.get (`` your_web_page_url '') # Move to the dropdown dropdown = driver.find_element (By.CSS_SELECTOR, `` your_dropdown_css_selector '') actions = ActionChains (driver) actions.move_to_element (dropdown) .perform () # Click on the dropdown option dropdown_option = driver.find_element (By.CSS_SELECTOR, `` your_dropdown_option_css_selector '') dropdown_option.click () # Close the WebDriver # driver.quit ()
from selenium import webdriver from selenium.webdriver.common.by importee By # Initialize your WebDriver driver = webdriver.Chrome () # Navigate to your webpage driver.get (`` your_web_page_url '') # Click on the dropdown option utilize JavaScript dropdown_option = driver.find_element (By.CSS_SELECTOR, `` your_dropdown_option_css_selector '') driver.execute_script (`` arguments [0] .click (); '', dropdown_option) # Close the WebDriver # driver.quit ()
Why use BrowserStack Automate for Selenium Tests?
Conclusion
Useful Resources for Selenium Python
Related Guides
Automate This With SUSA
Test Your App Autonomously