How To Select Drop Down in Selenium?

How to handle dropdown in selenium?

Dropdown handling is one of the most common challenges for everyone who is doing automation Selenium. 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 shared with descriptive details and snapshot.

Pre-Requisites:

  1. 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 very useful method to perform select related operations.

And, here are different ways to use select class in selenium:

  • Find the WebElement using selenium methods.

Example:- WebElement dropdown =driver.findElement(By.xpath(“//select[@id=’searchLanguage’]”));

  • Make object of Select class by passing above WebElement as constructor

Example:- Select drpdown = new Select(dropdown);

Methods available for Select dropdown operation in Selenium:

Selenium Select methods
Selenium Select methods

Select dropdown operation using selenium example

  • Step 1: Open website and find xPath for desired WebElement

languageXpath= “//select[@id=’searchLanguage’];”

Wikipedia Automation Code
Wikipedia automation element identification
  • 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 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 value 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 value from drop down list in selenium webdriver?

  • Step 8: Use SelectByValue to select dropdown by Value
Thread.sleep(7500); // wait for 7.5 seconds. Just to see visible
drpdown.selectByValue("hu");
Select by value
  • Step 9: Select by selectByVisibleText- Select class gives method selectByVisibleText to select dropdown by visible text.
drpdown.selectByVisibleText("Suomi");
SelectByVisibleText
  • Step 10: 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 11: 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 12: Verify Actual Result with Expected Result – Testng provides 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();
    }
}

Hope this article helps you in dropdown related testing. Still, if you face any issue please feel free to contact us in 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 & few monthly newsletter.

Subscribe to our Newsletter:

0 0 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact