Have a question?
Message sent Close
View Categories

Handling the drop downs and multi-select dropdown

📄
filename.js
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 DropdownExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your <select> dropdown

            driver.get("https://example.com/registration");

            // 4. Locate the <select> element by XPath

            WebElement countrySelectElem = driver.findElement(

                By.xpath("//select[@id='countryDropdown']"));

            // 5. Wrap it in a Selenium Select helper

            Select countrySelect = new Select(countrySelectElem);

            // 6. Basic checks

            System.out.println("Dropdown displayed? " + countrySelectElem.isDisplayed());

            System.out.println("Dropdown enabled?   " + countrySelectElem.isEnabled());

            // 7. Select by visible text

            countrySelect.selectByVisibleText("United Kingdom");

            Thread.sleep(1500);

            System.out.println("Selected (by text): " +

                countrySelect.getFirstSelectedOption().getText());

            // 8. Select by value attribute

            countrySelect.selectByValue("US");

            Thread.sleep(1500);

            System.out.println("Selected (by value): " +

                countrySelect.getFirstSelectedOption().getAttribute("value"));

            // 9. Select by index

            countrySelect.selectByIndex(3);

            Thread.sleep(1500);

            System.out.println("Selected (by index): " +

                countrySelect.getFirstSelectedOption().getText());

        } finally {

            // 10. Close the browser

            driver.quit();

        }

    }

}

📄
filename.js
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 java.time.Duration;

import java.util.List;

public class MultiSelectDropdownExample {

    public static void main(String[] args) throws InterruptedException {

        // 1. Point to your ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // 2. Launch Chrome

        WebDriver driver = new ChromeDriver();

        try {

            // 3. Navigate to the page containing your multi-select dropdown

            driver.get("https://example.com/multiselect");

            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

            // 4. Locate the <select> element using an XPath locator

            WebElement multiSelectElem = driver.findElement(

                By.xpath("//select[@id='fruitsSelect']"));

            // 5. Wrap it in the Selenium Select helper

            Select multiSelect = new Select(multiSelectElem);

            // 6. Verify that it supports multiple selections

            System.out.println("Is multi-select? " + multiSelect.isMultiple());

            if (!multiSelect.isMultiple()) {

                System.out.println("Dropdown is not multi-select, exiting.");

                return;

            }

            // 7. Select multiple options

            multiSelect.selectByVisibleText("Apple");

            Thread.sleep(1000);

            multiSelect.selectByValue("orange");    // <option value="orange">Orange</option>

            Thread.sleep(1000);

            multiSelect.selectByIndex(3);           // selects the fourth item

            Thread.sleep(1000);

            // 8. Retrieve and print all selected options

            List<WebElement> selectedOptions = multiSelect.getAllSelectedOptions();

            System.out.println("Currently selected items:");

            for (WebElement option : selectedOptions) {

                System.out.println(" • " + option.getText());

            }

            // 9. Deselect a single option by value/text/index

            multiSelect.deselectByVisibleText("Orange");

            Thread.sleep(1000);

            System.out.println("After deselecting Orange:");

            for (WebElement option : multiSelect.getAllSelectedOptions()) {

                System.out.println(" • " + option.getText());

            }

            // 10. Deselect all

            multiSelect.deselectAll();

            Thread.sleep(500);

            System.out.println("After deselectAll, selected count: " 

                + multiSelect.getAllSelectedOptions().size());

        } finally {

            // 11. Close the browser (end the session)

            driver.quit();

        }

    }

}