Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send keyboard keys combination in selenium webdriver (java)?

I want to send 1999 to a text box in Selenium WebDriver (java). The following code is not working when I try to combine the key strokes into a string before sendkeys:

String allKeys = Keys.NUMPAD1 + Keys.NUMPAD9 + Keys.NUMPAD9 + Keys.NUMPAD9; 

Am getting this error:

The operator + is undefined for the argument type(s) org.openqa.selenium.Keys, org.openqa.selenium.Keys

like image 237
Ibexy I Avatar asked Sep 23 '12 12:09

Ibexy I


People also ask

What is the Selenium WebDriver command to use following combination of keys on the keyboard Ctrl A?

We can also perform a CTRL+A press by simply using the sendKeys() method. We have to pass Keys. CONTROL along with the string A by concatenating with +, as an argument to the method.

How many ways we can send keys in Selenium?

Now, as we discussed, Selenium WebDriver provides two ways to send any keyboard event to a web element: sendKeys() method of WebElement class. Actions class.


4 Answers

Instead of using:

String allKeys = Keys.NUMPAD1 + Keys.NUMPAD9 + Keys.NUMPAD9 + Keys.NUMPAD9; 

You should use:

driver.findelement(by.xpath(xpathExpr)).sendkeys(Keys.NUMPAD1, Keys.NUMPAD9, Keys.NUMPAD9, Keys.NUMPAD9);

Or use:

String allKeys = "1999";
driver.findelement(by.xpath(xpathExpr)).sendkeys(allKeys);
like image 171
Sam Vo Avatar answered Sep 30 '22 19:09

Sam Vo


why not use send keys.

driver.findelement(by.xpath(xpathExpr)).sendkeys("1999");
like image 40
gSr Avatar answered Sep 29 '22 19:09

gSr


Try this. It works for me!

driver.findelement(by.xpath(xpathExpr)).SendKeys(keys.NumberPad1+keys.NumberPad9+keys.NumberPad9+keys.NumberPad9);
like image 21
Syed Ali Mesam Avatar answered Sep 30 '22 19:09

Syed Ali Mesam


Question : How do I send keyboard keys combination in selenium webdriver (java)?

Answer : You can send keyboard keys using below method

Method 1 :

driver.findElement(By.id("Year")).sendKeys(Keys.NUMPAD9);

Method 2 :

String allKeys = "1999";

driver.findElement(By.id("Year")).sendKeys(allKeys);

Method 3 :

driver.findElement(By.id("Year")).sendKeys("1999");

enter image description here

like image 32
sandeep shewale Avatar answered Oct 01 '22 19:10

sandeep shewale