How to handle dropdown in Selenium Python?

Related Product On This Page The Need for a DropdownJune 19, 2026 · 11 min read · Tool Comparison

Related Product

How to handle dropdown in Selenium Python?

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.

The Need for a Dropdown

  • The independent reward of using a dropdown is that it saves your apps/web pages from unnecessary space consumption by contain all the options within it.
  • It eliminates the problems like mistyping, misspelling, or wrong stimulation from the users.
  • It aid find peculiar content on a web page without scroll up and down.
  • Save exploiter ’ clip on a large website. You can access your targeted message without wasting any time.
  • It sets the limit of options to opt from and assist users well navigate.
  • Preserve screen space from unwanted content.
  • Also helps to sail to another page.

What is Select Class in Selenium?

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:

Why Use Select Class in Selenium Python To Handle Dropdowns?

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:

  • Select/deselect multiple value
  • Use deselect_all () to reset choice

5. Provides Access to All Options:You can easy regain all dropdown options with select.options, useful for validation and dynamic checks.

Read More:

How to handle dropdown in Selenium Python

Prerequisites

1. First, install Python. Use Command-

py –m pip install –U pip

2.Install Selenium in a Python surround. Run the command-

pip install selenium

3.Then meaning Selenium WebDriver and Keys classes.

from selenium import webdriver from selenium.webdriver.common.keys import Keys

After that, start your examination.

How to Select Option in Dropdown using Selenium Python

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

'selenium.webdriver.support.select.Select '

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.

// Create an objective of the Select class Select select = new Select (driver.findElement (By.xpath (`` // * [@ id='oldSelectMenu '] '')));

There are multiple methods to choose dropdowns in Selenium Python. They are:

select_by_index (int index)

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-

select.select_by_index (2);

select _by_value (String value)

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-

select.select_by_value (‘ Selenium Python ’);

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

select _by_visible_text (String text)

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-

select.select_by_visible_text (‘ Selenium Python ’);

Let us see an example of care dropdown with the above three methods-

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 ();}}
  • deselect_by_index (int indicant)

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 ’.

select.deselect_by_index (1);
  • deselect _by_value (String value)

It deselects the value which matches the method controversy.

select.deselect_by_value (‘ Selenium Python ’);
  • deselect _by_visible_text (String schoolbook)

It deselects the textbook which matches the method argumentation.

select.deselect_by_visible_text (‘ Selenium Python ’);

Test Multiple-Select dropdown

The Select family provides a‘ isMultiple () ’method. It has the features like:

  1. Select multiple values by calling the method several times for multiple values.
  2. Validate whether the dropdown allows multiple value or not.
  3. Returns a Boolean value- true/false.
  4. By use multi-select elements you can implement other select methods.

Selenium Webdriver provides four types of methods to test whether your drop-down supports multi-select operation or not. They are-

  • options()
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 ()

Returns the first choose pick from the dropdown.

first_selected_option (): WebElement // Get the first selected option of the dropdown WebElementFirstSelectedOption = select.getFirstSelectedOption ();
  • all_selected_options ()

Returns all the selected options.

all_selected_options (): List & lt; WebElement & gt; // Get all the selected option of the dropdown List & lt; WebElement & gt; selectedOptions = select.getAllSelectedOptions ();
  • deselect_all ()

Clears all the choose options. If the option doesn ’ t support multi-select so the Python Selenium will shed the ‘NotImplementedError’ elision.

deselect_all (): void //Deselect all the option select.deselect_all ();

Note: The first three method are valid for both individual and multi-select. But thedeselect_all ()method is solely valid for multi-select.

Handling multi-select options in dropdown with Selenium Python

Here is a sample code to treat a multi-select element dropdown covering the above-mentioned methods:

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 ();}}

Run Selenium Tests on BrowserStack Automate

  • provides a reliable Selenium try infrastructure that go highly accelerated by supporting of more than 10x of your test entourage.
  • It facilitates the debugging with video recordings, automated screenshots of error, text logarithm, console logs, and network logs.
  • Run the tests within a highly secured environment.

How To Handle Dropdowns in Selenium Python Without Select Class?

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.

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 ()

2. Using ActionChains

In scenario like dealing with hover-based dropdowns, you can use ActionChains.

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 ()

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.

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 ()

Read More:

Why use BrowserStack Automate for Selenium Tests?

Here are ground why you should use BrowserStack Automate for Selenium Tests:

  • Parallel Testing:BrowserStack Automate lets you test on multiple device and browser combinations at once, speeding up exam execution and providing spry feedback.
  • Real Devices and Browsers:Testing on real devices and browser ensures accurate performance insights, unlike emulators. You can test on the late devices without bribe them.
  • Dedicated Dashboard:Automate offers a splasher to track and manage your exam, show trial status (Pass/Fail/Pending), device details, test continuance, screenshots, and more.
  • Custom Reports with Artifacts:Generate detailed, customized reports include test status, device/browser configurations, picture recordings, and screenshots.
  • Easy Integration with CI/CD:Seamlessly integrate with CI/CD tools like Jenkins, TeamCity, and TravisCI for faster, reliable, and consistent application delivery.

Talk to an Expert

Conclusion

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.

Useful Resources for Selenium Python

Tags
20,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free