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.
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();
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();
In the case of using Mac, te code would be next:
action.keyDown(Keys.COMMAND)
.click(WebElement)
.keyUp(Keys.COMMAND)
.build()
.perform();
You achieve same using jquery code
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "e = jQuery.Event('click');e.ctrlKey = true; $('secondRow_Css_locator').trigger(e);";
js.executeScript(script);
OR you can also use robot class but it can lock your screen for a moment
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
As of 2018 the is the first results pops up. Earlier it was working fine after FF 61 (direct jump form 47 to 61) It starts breaking. unfortunately none of the answer worked for me. Solved it by action.keyDown(Keys.CONTROL).click(myWebElements.get(i)).keyUp(Keys.CONTROL).perform(); just iterating every element one by one
it worked with me using this:
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();
Related
how can I press a combination of buttons using selenium in chrome?
I have an extension which opens a new tab by pressing control + shift + x but how can I use the shortcut of this extension in selenium?
I have tested many things but they didnt work for me.
Thx for every answer :)
Try the following code:
It send series of key codes to the body element of the webpage.
driver.findElement(By.tagName("body")).sendKeys(Keys.LEFT_CONTROL, Keys.LEFT_SHIFT, "x");
Alternative is Actions
Actions builder = new Actions(driver);
Action seriesOfActions = builder
.keyDown(Keys.LEFT_CONTROL)
.keyDown(Keys.LEFT_SHIFT)
.sendKeys("x")
.build();
seriesOfActions.perform();
Could like to press character 'v' in keyboard through robot which works fine as expected in browser mode but not working in headless mode .
Trying to loop throught list of elements and screenshot it post keypress . I am using Robot class to press the character 'v' which is not working instead it prints v in output.
I am using firefox- geckodriver-v0.24.0-win64 driver and my browser version is v69.0.1 .
List<WebElement> eleq = driver.findElements(By.cssSelector(".class"));
JavascriptExecutor js = ((JavascriptExecutor) driver);
for(WebElement e: eleq){
js.executeScript("arguments[0].scrollIntoView(true);", e);
Actions builder = new Actions(driver);
Action seriesOfActions = builder
.moveToElement(e)
.build();
seriesOfActions.perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_V);
Thread.sleep(1000);
Date d =new Date();
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File(d.toString().replace(":", "_")+".png"));
Thread.sleep(3000);
robot.keyPress(KeyEvent.VK_V);
}
Could like to know if there are any alternatives to keypress in Java selenium apart from Robot since most of my search led to Robot not working in headless.
Of-course it will not as Robot do the event without seen any other dependency.
Even if you minimize the your automation browser screen it won't work as it is independent from everything
That's why using Robot is not recommended in automation.
You need to identify other way to complete your step
you can do something like below:
String selectAll = Keys.chord(Keys.CONTROL, "a");
driver.findElement(By.id("your locator")).sendKeys(selectAll);
I have achieved this using Actions in selenium.
Selenium Webdriver v-3.0.1
I want to close the browser window using hot keys, I have tried following methods one by one but not working at all -
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL+"w"));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL+"F4"));
driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL,Keys.F4));
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL, Keys.F4);
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL+"F4");
How do I close browser other then driver.close() and driver.quit method
Make sure browser window should be active.
You can perform using actions
Actions actions = new Actions(driver);
actions.keyDown(Keys.ALT);
actions.sendKeys(Keys.F4);
actions.keyUp(Keys.ALT);
actions.perform();
Same you can achieve using Robot.
To get this code running you need to add,
import java.awt.Robot;
Robot robot = new Robot();
// press key Alt+F4
robot.keyPress(KeyEvent.VK_ALT);
robot.delay(100);
robot.keyPress(KeyEvent.VK_F4);
// relase key Alt+F4
robot.delay(100);
robot.keyRelease(KeyEvent.VK_F4);
robot.delay(100);
robot.keyRelease(KeyEvent.VK_ALT);
If you do not use driver.quit() at the end of program, WebDriver session will not close properly and files would not be cleared off memory. This may result in memory leak errors.
Hope it is clear.
Assuming you have opened a single tab you can try below method:
driver.findElement(By.xpath("/html/body")).sendKeys(Keys.CONTROL+"w");
Actions actions = new Actions(driver);
String killBrowser= Keys.chord(Keys.ALT, Keys.F4);
actions.sendKeys(killBrowser);
actions.perform();
Try this.
How to write selenium java code for doubleClick() on a record using web driver?
I have displayed some records in the body part. Once I clicked on a record we should get a popup window to update it.
Please suggest how to write Selenium Java code using web driver.
I have tried following code:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().build().perform();
Use Actions class to perform mouse, keyboard actions on WebElements using WebDriver.
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.linkText("TEST"));
//Double click
action.doubleClick(element).perform();
//Mouse over
action.moveToElement(element).perform();
//Right Click
action.contextClick(element).perform();
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//table/tbody/tr[2]/td/div/div/table/tbody/tr[10]/td[1]"))).doubleClick().perform();
This code works!!!
You should use the Actions() class as this includes a 'double-click' action.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Test"))).doubleClick().build().perform();
Try this code:
Actions action = new Actions(driver);
WebElement btnElement=driver.findElement("Locator of element"));
action.doubleClick(btnElement).build().perform();
You can make use of Actions class of WebDriver to perform composite actions like
Double click, Drag and Drop, Hover etc.
// Creates an instance of Actions class, passing current driver instance.
Actions builder = new Actions(driver);
Way 1:
// Gets an Action class object that holds an action/ a set of actions
Action action = builder.doubleClick(element);
// Builds the set of actions/ single action using build() and executes on browser using perform() method.
action.build().perform();
Way 2:
// Calls build() and perform() methods directly on Actions class instance
builder.doubleClick().build().perform();
And in case if have no additional actions binded to singleclick, you can use:
driver.findElement(By.xpath("%youXPath%"))).click;
driver.findElement(By.xpath("%youXPath%"))).click;
Actually, it should works in most cases (except you have some custom system doubleclick settings)
WebElement element = driver.findElement(selector);
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
I implemented Ran's (immediately above my post) solution. I'm writing Java in Eclipse and using Selenium WebDriver.
There are 2 imports that you'll need:
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
Then, I implemented the code thusly:
WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/div/div[2]/div[1]/div[3]/div[8]/div[2]/div/div[2]/div/table/tbody/tr[2]"));
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
Thanks to Ran!
I'd been struggling with this one for several hours. Invoking the single click twice doesn't work for me - too much time between the events to be captured by the browser under test as a double-click.
I want to send the keys Ctrl and zero using Selenium WebDriver APIs
I tried the below code but not working
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys("F000").keyUp(Keys.CONTROL).perform();
Looking for help
Both these work for me:
A nice WebDriver approach
String ctrlZero = Keys.chord(Keys.CONTROL, "0");
driver.findElement(By.tagName("html")).sendKeys(ctrlZero);
and the pure Java approach working on a higher level:
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_0);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_0);
You need to use unicode characters , I guess this will work -
You must make this change in your code -
Actions action =new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0030')).perform();
Let me know if are facing any problems after this change.You can check the unicode table here -
http://unicode.org/charts/PDF/U0000.pdf