Key press in (Ctrl + mouse click) in Selenium WebDriver using java
Solution 1
There is already written library Actions in WebDriver which you can use.
Short Description of what is happening:
First you are pressing the Control button and then you are clicking (in this case) 3 times on your defined WebElemen objects) then your are unpressing the Control and finish your Actions.
In this case you can achive the selection of 3 items (or opening a 3 new tabs) depending on what your WebElements are.
Actions actions = new Actions(driver);
actions.keyDown(Keys.LEFT_CONTROL)
.click(first_WebElement)
.click(second_WebElement)
.click(third_WebElement)
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();
Solution 2
Do it with the help of 'Actions' as below:
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform();
driver.findElement(By.xpath(".//*[@id='selectable']/li[1]")).click();
driver.findElement(By.xpath(".//*[@id='selectable']/li[3]")).click();
action.keyUp(Keys.CONTROL).build().perform();
Solution 3
In the case of using Mac, te code would be next:
action.keyDown(Keys.COMMAND)
.click(WebElement)
.keyUp(Keys.COMMAND)
.build()
.perform();
![Hetal Patel](https://i.stack.imgur.com/UfXqa.jpg?s=256&g=1)
Hetal Patel
Updated on July 06, 2020Comments
-
Hetal Patel almost 4 years
I need to press control+mouse click keys using Selenium WebDriver(java). I need to select multiple element in my script. Is there any way to do it?
I checked the Selenium libraries and found that selenium allows key press of special and functional keys only.
-
lenz almost 9 yearsSo think the documentation is lying? Or what is your actual question?
-
JeffC almost 9 yearsWelcome to Stack Overflow! Please read the guide How do I ask a good question, especially the part on Minimal, Complete, and Verifiable example (MCVE). This will help you solve problems for yourself. If you do this and are still stuck you can come back and post your MCVE, what you tried, and what the results were so we can better help you.
-
-
John Chesshir over 3 yearsLove this answer! It carries with it the ability to loop through a variable list of items to click, which separates the syntax from the functionality. I think it should be the accepted answer.