Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key press in (Ctrl + mouse click) in Selenium WebDriver using java

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.

like image 644
Hetal Patel Avatar asked Nov 30 '22 00:11

Hetal Patel


2 Answers

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();
like image 89
Arsey Avatar answered Dec 09 '22 19:12

Arsey


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();
like image 29
Deepak Arockiaraj Avatar answered Dec 09 '22 17:12

Deepak Arockiaraj