How to Handle DropDown in Selenium WebDriver

How to Handle DropDown in Selenium WebDriver

What we have learned so far?

Part 1 – What is Selenium WebDriver? How to setup Selenium?


Part 2 – Locators in Selenium WebDriver – Id, Name, Class Name, Tag Name, CSS, XPath

https://www.onlyfullstack.com/locators-in-selenium-webdriver/

Part 3 – Selenium WebDriver Commands

What is Select in Selenium WebDriver?

We can handle the Dropdown with Select class in Selenium WebDriver. Lets see how to handle the dropdown. The ‘Select’ class in Selenium WebDriver is used for selecting an option in a dropdown.

WebElement countryDropDownElement = webDriver.findElement(By.name("Country"));
Select countryDropDown = new Select(countryDropDownElement);

How to select option from dropdown in selenium?

WebDriver provides three ways to select an option from the drop-down menu.

1. Select By Visible Text
It is used to select an option based on the text over the option.

countryDropDown.selectByVisibleText("India");

2. Select By Value
It is used to select an option based on its ‘value’ attribute.

countryDropDown.selectByValue("2");

3. Select By Index
It is used to select an option based on its index, beginning with 0.

countryDropDown.selectByIndex(3);

Below is the complete program to select a dropdown value with different ways –

public class DropDownOperations {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html");

        webDriver.manage().window().maximize();
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("window.scrollBy(0,2200)");

        selectDropDownValue(webDriver);

        webDriver.close();
    }

    private static void selectDropDownValue(WebDriver webDriver) throws InterruptedException {
        WebElement countryDropDownElement = webDriver.findElement(By.name("Country"));
        Select countryDropDown = new Select(countryDropDownElement);

        countryDropDown.selectByVisibleText("India");
        Thread.sleep(1000);

        countryDropDown.selectByValue("2");
        Thread.sleep(1000);

        countryDropDown.selectByIndex(3);
        Thread.sleep(1000);
    }
}

How to get the values from DropDown in selenium?

We can get the values of a drop down list from the select.getOptions() method.

public class DropDownOperations {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com/2020/03/best-demo-website-to-practice-selenium.html");

        webDriver.manage().window().maximize();
        JavascriptExecutor js = (JavascriptExecutor) webDriver;
        js.executeScript("window.scrollBy(0,2200)");

        //selectDropDownValue(webDriver);
        getAllDropDownValues(webDriver);
        webDriver.close();
    }

    private static void getAllDropDownValues(WebDriver webDriver) throws InterruptedException {
        WebElement countryDropDownElement = webDriver.findElement(By.name("Country"));
        Select countryDropDown = new Select(countryDropDownElement);

        List<WebElement> dropDownList = countryDropDown.getOptions();

        System.out.println("DropDown values - ");
        for(WebElement dropDown : dropDownList) {
            System.out.println(dropDown.getText());
        }
    }
}

Output-

DropDown values -
Choose
India
UK
USA

Lets go to our next tutorial where we will discuss below points :

Part 6 – How to handle alerts, confirm, prompts and popups in Selenium WebDriver

– What is Alert?
– Types of Alert
– 1. Simple Alert
– 2. Confirmation Alert

     – 3. Prompt Alert
– How to handle Alert / Confirm / Prompt in Selenium WebDriver?
– 1. void accept()
– 2. void dismiss()
– 3. String getText()

– 4. void sendKeys(String stringToSend)

Source Code
You can find the complete source code on below GitHub repository – 
https://github.com/onlyfullstack/selenium-tutorial

Selenium Tutorial
https://www.onlyfullstack.com/selenium-webdriver-tutorial/