How to handle dropdown in selenium?
Select drop down using selenium!! Although this may be true, dropdown handling is one of the most common challenges for everyone who is doing automation Selenium. With this purpose in mind of making this task easier we have come up with this tutorial, Basically, this tutorial helps you to select dropdown by index, visible text, and value. Verify selected dropdown, to get all the available dropdown and count the dropdowns.
In this tutorial, we will learn about Selenium select class and their available methods with an example, line by line code explanation, and complete code. Also, each step is shared with descriptive details and snapshots.
Pre-Requisites:
- The first thing to remember, Selenium must be set up
Read More: How to setup selenium step by step?
How to use Selenium Select class with example?
Selenium “Select” provides a very useful method to perform select related operations.
And, here are different ways to use select classes in selenium:
- At first, find the WebElement using selenium methods.
Example:- WebElement dropdown =driver.findElement(By.xpath(“//select[@id=’searchLanguage’]”));
- Correspondingly, make object of Select class by passing above WebElement as constructor
Example:- Select drpdown = new Select(dropdown);
Following methods available for Select dropdown operation in Selenium:
Select dropdown operation using selenium example
- Step 1: To begin with, open the website and find xPath for the desired WebElement
languageXpath= “//select[@id=’searchLanguage’];”
- Step 2: Open Selenium Project and create one class and write one test method
Selenium code to open browser:
- Step 3: Write code to open the browser, navigate to URL and store select web element
WebDriver driver = new ChromeDriver();
driver.get("https://www.wikipedia.org/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// store dropdown as Webelement
WebElement dropdown = driver.findElement(By.xpath("//select[@id='searchLanguage']"));
- Step 4: Make object of Select class
Select drpdown = new Select(dropdown);
How to get all dropdown values in selenium webdriver?
- Step 5: To count total number of dropdown options: use method – getOptions().size()
int totalNoOfDropdown = drpdown.getOptions().size();
System.out.println("Total Number of Dropdown:" + totalNoOfDropdown);
// Verify total number of language with expected count
Assert.assertEquals(totalNoOfDropdown,66, "Verified total number of dropdowns ");
How to get the selected value of dropdown in selenium webdriver?
- Step 6: To get first selected Options : getFirstSelectedOption(). If dropdown is single selected then it’s return current selected dropdown and use getText() method.
WebElement selectedDropdown = drpdown.getFirstSelectedOption();
String selectedDropdowntext = selectedDropdown.getText();
Select Drop Down by Index in selenium:
- Step 7: Select dropdown by Index: use selectByIndex method to select dropdown by index
// Select dropdown using index - Index 9 -Dansk
drpdown.selectByIndex(9);
How to select a value from the drop down list in selenium webdriver?
- Afterwards, use SelectByValue to select dropdown by Value
Thread.sleep(7500); // wait for 7.5 seconds. Just to see visible
drpdown.selectByValue("hu");
- Step 8: Select by selectByVisibleText- Select class gives method selectByVisibleText to select dropdown by visible text.
drpdown.selectByVisibleText("Suomi");
- Step 9: Check whether dropdown is multiple select or not? – Method isMultiple() return true when dropdown is multiselect or else return false.
// Check whether dropdown is multiple select or not
System.out.println("is this dropdown is multiple select : "+drpdown.isMultiple());
- Step 10: Get all the Options: getOptions() method returns all the WebElements
// Print all the dropdowns
List allOptions = drpdown.getOptions();
for (WebElement ele : allOptions) {
System.out.println(ele.getText());
- Step 11: Lastly, verify Actual Result with Expected Result – Testng provides a wide range to assert methods using those methods we can assert.
Selenium + Java Code for Select Drop Down using different methods example:
package stepDef; import io.github.bonigarcia.wdm.WebDriverManager; 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; import org.testng.Assert; import org.testng.annotations.Test; import java.util.List; import java.util.concurrent.TimeUnit; public class TestDropdown { @Test public void dropdownTest() throws InterruptedException { WebDriverManager.chromedriver().version("79.0").setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.wikipedia.org/"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // store dropdown as Webelement WebElement dropdown = driver.findElement(By.xpath("//select[@id='searchLanguage']")); // Use Selenium "Select" class to perform dropdown related operations Select drpdown = new Select(dropdown); //Count total dropdowns int totalNoOfDropdown = drpdown.getOptions().size(); System.out.println("Total Number of Dropdown:" + totalNoOfDropdown); // Verify total number of dropdowns Assert.assertEquals(totalNoOfDropdown, 66, "Verified total number of dropdowns "); // verify selected Options WebElement selectedDropdown = drpdown.getFirstSelectedOption(); String selectedDropdowntext = selectedDropdown.getText(); System.out.println("Selected Dropdown: " + selectedDropdowntext); // Verify selected Dropdown using Testng Assert Method Assert.assertEquals(selectedDropdowntext, "English", "Verified selected Dropdown"); // Select dropdown using index - Index 9 -Dansk drpdown.selectByIndex(9); // Select dropdown by using value-(Select Language- Magyar using value- "hu" Thread.sleep(7500); // wait for 7.5 seconds. Just to see visible drpdown.selectByValue("hu"); // Select dropdown using Visble text "Suomi" - Language Suomi will be selected Thread.sleep(5000); drpdown.selectByVisibleText("Suomi"); // Check whether dropdown is multiple select or not System.out.println("is this dropdown is multiple select : " + drpdown.isMultiple()); // Print all the dropdowns List<WebElement> allOptions = drpdown.getOptions(); for (WebElement ele : allOptions) { System.out.println(ele.getText()); } driver.close(); } }
To this end, hope this article helps you in dropdown-related testing. Still, if you face any issue please feel free to contact us at query@thoughtcoders.com or message us on 9555902032.
References:
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/Select.html
Signup to our Newsletter
Our newsletter includes post updates, tips & a few monthly newsletters.