How to select multiple values from a list webdriver using Actions (Keys, keyDown, keyUp)

unlike select element where we can select only one single value, from list we can select multiple values by clicking on the values by holding CTRL / SHIFT key.

And to perform click method using mouse and hold CTRL / SHIFT key using keyboard these methods we can perform using Actions Interface.

suppose there is a list like below in the html code

      
               

                    
  • Australia

  •                
  • India

  •                
  • Canada

  •                
  • USA

  •                 

        

the the code might be like below

FireFoxDriver driver=new FirefoxDriver();
driver.get("..........url..........");

         ///////find the ul element and store in WebElement
WebElement ctry=driver.findElement(By.id("country"));

            /////get all the li items and store in List collection
List lst=ctry.findElements(By.tagname("li"));

             //////use Actions to build composite actions and perform togeather
             ///////down the ctrl key, click on 0, 3 items in the list and release the control key, all the actions are performed.
new Actions(driver)
            .keyDown(ctry,Keys.CONTROL)
            .click(lst.get(0))
            .click(lst.get(3))
            .keyUp(ctry,Keys.CONTROL)
            .perform();





....good luck

Comments