I just upgraded to Selenium 2 and having trouble simulating a keypress in firefox (maybe other browsers?). First of all, the new API, using IWebDrivers, does not provide a keypress funktion. I can acquire a ISelenium in开发者_如何转开发stance with the 1.0 API (WebDriverBackedSelenium) functions, however I receive an error when using this. E.g.
new WebDriverBackedSelenium(driver, TestServerUrl).KeyDownNative("27");
yields
System.NotSupportedException : keyDownNative
The same is the case for KeyDown, KeyPress etc. Is this not supported in Selenium v2?
Thanks in advance!
/Jasper
Ok so to future reader - I read some of ThoughtWorks documentation and the Selenium v2 API is not quite implemented yet.
So note to self - Big difference between v1 and v2 and v2 API not fully implemented.
To send key presses to an WebElement using Selenium 2 (i.e. to an input field) you can do the following:
// Retrieve the required WebElement object of interest //
WebElement myElement = getWebelement();
// send some chars
myElement.sendKeys("Some Test Text");
Also, to delete text from a WebElement (i.e. an input box) you could do the following:
String BACK_SPACE_UNICODE_CODE = "\u0008";
WebElement inputElement = getWebelement();
String currentValue = inputElement.getAttribute("value");
if (!"".equals(currentValue))
{
for (int count=0;count< currentValue.length();count++)
{
inputElement.sendKeys(BACK_SPACE_UNICODE_CODE);
}
}
Probably best to put this code within a function so it can be used throughout your tests.
精彩评论