Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  •  Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  •  Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  •  Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  •  Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  •  Handling the drop downs and multi-select dropdown

 Handling the drop downs and multi-select dropdown

 Handling the drop downs and

multi-select dropdown

Handling the drop down 

Test Case Flow: 

  1. Launch Chrome via WebDriver.
  2. Navigate to https://example.com/preferences.
  3. Locate the <select> element by XPath
  4. Check if the dropdown is displayed and enabled.
  5. Select the dropdown value using Select by visible text
  6. Select the dropdown value using Select by value attribute
  7. Select the dropdown value using Select by index
  8. Close the browser.

Program:

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

        }

    }

}

Handling the multi-drop down 

Test Case Flow: 

  1. Launch Chrome via WebDriver.
  2. Navigate to https://example.com/preferences.
  3. Locate the <select> element by XPath
  4. Verify that it supports multiple selections
  5. Retrieve and print all selected options
  6. Deselect a single option by value/text/index
  7. Deselect all
  8. Close the browser.

Program:

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

        }

    }

}

Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Handling the radio button and checkbox using seleniumValidate the text validation and attributes values. 
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105