Robot keypress not working in headless mode - java

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.

Related

How to open a new tab using an extension and selenium?

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();

After zoom out unable to find the webelement in selenium webdriver

In my app, I need to zoom out and then find out the web element.
I zoom out using Robot robot = new Robot(). It zoomed out successfully. After that, it doesn't find the web element. I tried with an explicit wait.
Robot robot = new Robot();
Thread.sleep(5000);
System.out.println("About to zoom out");
for (int i = 0; i < 3; i++) {
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
new WebDriverWait(driver,50).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(prop.getProperty("mpath"))))
.click();
WebElement module1 = driver.findElement(By.cssSelector(prop.getProperty("msite")));
module1.click() ;
I don't know your case exactly, but here's what I would try to do if I were you.
1. Try to zoom out the browser in a different way.
Instead of using Robot class, try to use:
element.sendKeys(Keys.chord(Keys.CONTROL, "-")) (Keys.COMMAND on MacOS).
You could also try out Actions class if this doesn't work.
2. Consider looking for that element again, after the resize.
Not sure how it works during resizing of a page, but I know that after the refresh, WebElement's id might change, which might cause your problem.
Anyway, if none of these work, try to post the error code so we can try to help you.

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.
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();

How to press "ALT+S" in Selenium WebDriver using Java

I need to Send ALT+S Key event using Selenium Web Driver for an ``EditBox. Cursor Position is already set to EditBox I am using following code
driver.switchTo().activeElement().sendKeys(Keys.chord(Keys.ALT+"S"))
but it's not giving me desired result. It's Typing character 'S' in the Edit Box.
I have tried another code but got the same result.
Actions action =new Actions(driver);
action.keyDown(Keys.ALT).sendKeys(String.valueOf('\u0053')).perform();
Thanks in Advance
I want to Add one more thing here. The code is working Properly in Firefox 12 but its not working properly in IE9
Cross-browser issues are rather hard to investigate as they are specific to particular driver and not WebDriver API.
Another variant that might work.
driver.findElement(By.xpath("your editbox's XPath")).sendKeys(Keys.chord(Keys.ALT, "s"));
As workaround I might recommend to take a look to AutoIT (Official site) or Robot (Java Doc)
Try this. It might work, I haven't tried though
driver.findElement(By.xpath("your editbox's XPath"))
.sendKeys(Keys.chord(Keys.ALT + Keys.S));
You can achieve this by using Robot class of java
try{
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

How to press Ctrl+0 (Zero) using Selenium WebDriver

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

Categories

Resources