How to select multiple options from a dropdown list using Actions class in Selenium?

0 votes
How to select multiple options from a dropdown list using Actions class in Selenium?
Jul 8 in Selenium by Renu
516 views

1 answer to this question.

0 votes

Hey Renu, you can use Actions class provided by Selenium, to select multiple options from a dropdown. Actions class interact with keyboard and mouse activities and you can select multiple option by pressing Ctrl and click on options. Checkout the following lines of code to understand better:

public class MultiSelectDropdown {

    WebDriver driver;

    @Test
    public void testApp() {

        System.setProperty("webdriver.chrome.driver", "D:\SeleniumDrivers\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        WebElement select1 = driver.findElement(By.xpath("//option[@value='Volvo']"));
        WebElement select2 = driver.findElement(By.xpath("//option[@value='Saab']"));

        Actions action = new Actions(driver);
        action.keyDown(Keys.CONTROL).click(select1).click(select2).build().perform();

    }

}
answered Jul 8 by Abha
• 25,240 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
780 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,025 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
726 views
0 votes
1 answer
0 votes
1 answer