How to press "ALT+S" in Selenium WebDriver using Java - 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());
}

Related

Alternate ways to handle Robot Class Steps in Selenium Test

My application supports only IE currently. In one case, it opens a pop up window with some fields which are not inspect-able. I am using Robot class features (tab sequences, mouse clicks, sendkeys) to enter data, do a search and other steps on that.
This scenarios works fine in my local but due to large volume of my scenarios we are running them in bulk on cloud machines where the Robot keys functions are not supporting.
Is there any alternate ways to handle this case to continue my scenarios running on virtual machines.
Have anyone faced such cases & can you share your experience handling it in selenium script
Robot class was the option tried which is working in local, need solution to run them on virtual machines.
Tried Switch to pop up window, frame - those are not working as well, the pop window is not getting identified itself (In the window its displayed as Search -- WebPage Dialog)
Below is a sample code snippet - i am using for a search function in the pop up using Robot Class
try {
Thread.sleep(5000);
sendTab(34);
sendRobotKey("enter");
String name ="ABC";
copyToClipboard(name);
Thread.sleep(2000);
cntrolVRobotKey();
Thread.sleep(2000);
sendRobotKey("enter");
Thread.sleep(2000);
sendTab(4);
Thread.sleep(2000);
sendTab(1);
sendRobotKey("enter");
}
catch (Exception e) {
e.printStackTrace();
}
}
Use AutoIt instead of robot class

How to open search box on web page using keyboard key (ctrl-f) in selenium web driver

How to open search box using windows keys ctrl+f in selenium webdriver:
Selenium doesn't provide a possibility to simulate keyboard actions. But to be able to do it, you can use a Robot class in Java.
You have not specified on which language are you developing. Thats why I have stored some useful links to different analogues of Robot:
For Python
For C#
FIRST INSTALL PYWINAUTO BY run pip install pywinauto in your python ide
from selenium import webdriver
from pywinauto.keyboard import SendKeys
import time
driver = webdriver.Chrome(executable_path="PATH OF CHROMEDRIVER")
driver.maximize_window()
driver.get("https://www.google.com")
time.sleep(5)
SendKeys("^F")
Finally, guys, I have found a solution, It is working fine for me, Thanks Andrei Suvorkov to suggest me read about robot class.
try {
Robot robot = new Robot();
// Simulate a mouse click
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
// ctrl + F
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_F);
// CTRL+F is now pressed
robot.keyRelease(KeyEvent.VK_F);
robot.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException e) {
e.printStackTrace();
}

Press ENTER key in Selenium WebDriver with java when element property not present

I am using selenium webdriver with Java to automate the webpages
When I enter the url, I am getting the authentication required dialog box
I am able to enter the username and password by configuring profile
but I am not able to click on OK button
Note: Not able to get the ok button property so am not able to
use the below code
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Is there any other way to press on ok button through webdriver?
You need to handle it as an alert box,
wait for popup to appear and click OK.
Below code waits up to a maximum of 10 seconds for the popup to be present and then accepts it by clicking OK. Although wait is optional.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Handling credential boxes is not possible directly using Selenium You can use the JAVA AWT robot class to press enter. This class is available in the java API itself.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Alternatively, you can use AutoIt or an image based testing tool like SIKULI http://www.sikuli.org.
Please note that when you are using these solutions, the workstation screen cannot be locked while running the test cases.
Try this code snippet:
driver.findElement(By.xpath("//body")).sendKeys(Keys.RETURN);
It will definitely 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 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