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

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

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

Robot keypress not working in headless mode

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.

How to tap on an element multiple times in a faster way through selenium, appium, java and android

I want to automate a scenario using selenium, java, appium and android for tapping on an element multiple times in a faster way then a button appears, I have tried through java code using for loop with selenium webdriver click/appium touchactions tap but nothing helped me.
If you want to perform multiple tap on the element.
Use the below code:
WebElement element=driver.findElement(By.id("someID"));
TouchAction actionOne = new TouchAction();
actionOne.tap(element);
actionOne.release();
TouchAction actionTwo = new TouchAction();
actionTwo.tap(element);
actionTwo.release();
MultiTouchAction action = new MultiTouchAction();
action.add(actionOne);
action.add(actionTwo);
action.perform();
Add as many taps as possible if you need more than two. This should work.

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

Categories

Resources