Your ultimate guide

Select Multiple options in the dropdown using selenium with java

Select Multiple Items in the dropdown:
    When the dropdown is developed by using the <select> tag, then we can use the select class methods to automate single or multi-select dropdowns.
    In selenium, there is no specific method to Select multiple items in the dropdown. By using keyboard actions we can select multiple options in dropdown.

Example:
class Example
{
    public static void main(String args[])
    {
        System.setProperty("webdriver.chrome.driver" , "./chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://");
        
        WebElement mDropdown = driver.findElement(By.name("1234")); 
        Select s = new Select(mDropdown);

        Actions a = new Actions(driver);
        a.keyDown(Keys.CONTROL)
            .click(s.getOptions().get(2))
            .click(s.getOptions().get(3))
            .click(s.getOptions().get(4))
            .keyUp(Keys.CONTROL)
            .build().perform();
    }
}

isMultiple():
    We can use this method to ensure whether the specified dropdown is a multi-select dropdown or not. This method returns True when the corresponding dropdown is multi-select.

Example:
WebElement ele = driver.findElement(By.id("tcg"));
Select s = new Select(ele);

if(s.isMultiple())  {
    System.out.println("The Dropdown is Multi-Select");
}
else  {
    System.out.println("The Dropdown is not Multi-Select")
}

- Next Page: FindElements

No comments:

Post a Comment