I\'m looking for a quick way to type and Enter or Return key in Selenium. Unfortunately the form I\'m trying to test (not my own code so I can\'t modify) doesn\'t have a Submit button. When working with it manually, I just type ENTER or RETURN. I need to know how to do that with the Selenium type
command as there is no button to click.
问题:
回答1:
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
the import
statement is for Java, for other languages it is maybe a different, for example python: from selenium.webdriver.common.keys import Keys
回答2:
JAVA
driver.findElement(By.id(\"Value\")).sendKeys(Keys.RETURN);
OR,
driver.findElement(By.id(\"Value\")).sendKeys(Keys.ENTER);
PYTHON
from selenium.webdriver.common.keys import Keys
driver.find_element_by_name(\"Value\").send_keys(Keys.RETURN)
OR,
driver.find_element_by_name(\"Value\").send_keys(Keys.ENTER)
OR,
element = driver.find_element_by_id(\"Value\")
element.send_keys(\"keysToSend\")
element.submit()
RUBY
element = @driver.find_element(:name, \"value\")
element.send_keys \"keysToSend\"
element.submit
OR,
element = @driver.find_element(:name, \"value\")
element.send_keys \"keysToSend\"
element.send_keys:return
OR,
@driver.action.send_keys(:enter).perform
@driver.action.send_keys(:return).perform
C#
driver.FindElement(By.Id(\"Value\")).SendKeys(Keys.Return);
OR,
driver.FindElement(By.Id(\"Value\")).SendKeys(Keys.Enter);
回答3:
Now that Selenium 2 has been released, it\'s a bit easier to send an Enter key, since you can do it with the send_keys
method of the selenium.webdriver.remote.webelement.WebElement
class (this example code is in Python, but the same method exists in Java):
>>> from selenium import webdriver
>>> wd = webdriver.Firefox()
>>> wd.get(\"http://localhost/example/page\")
>>> textbox = wd.find_element_by_css_selector(\"input\")
>>> textbox.send_keys(\"Hello World\\n\")
回答4:
When writing HTML tests, the ENTER key is available as ${KEY_ENTER}
.
You can use it with sendKeys
, here is an example:
sendKeys | id=search | ${KEY_ENTER}
回答5:
You can use either of Keys.ENTER
or Keys.RETURN
. Here are some details :
Usage :
Java:
Using
Keys.ENTER
:import org.openqa.selenium.Keys; driver.findElement(By.id(\"element_id\")).sendKeys(Keys.ENTER);
Using
Keys.RETURN
import org.openqa.selenium.Keys; driver.findElement(By.id(\"element_id\")).sendKeys(Keys.RETURN);
Python:
Using
Keys.ENTER
:from selenium.webdriver.common.keys import Keys driver.find_element_by_id(\"element_id\").send_keys(Keys.ENTER)
Using
Keys.RETURN
from selenium.webdriver.common.keys import Keys driver.find_element_by_id(\"element_id\").send_keys(Keys.RETURN)
Keys.ENTER
and Keys.RETURN
both are from org.openqa.selenium.Keys
, which extends java.lang.Enum<Keys>
and implements java.lang.CharSequence
Enum Keys
Enum Keys is the representations of pressable keys that aren\'t text. These are stored in the Unicode PUA (Private Use Area) code points, 0xE000-0xF8FF.
Key Codes :
The special keys codes
for them are as follows :
- RETURN =
u\'\\ue006\'
- ENTER =
u\'\\ue007\'
The implementation of all the Enum Keys
are handled the same way.
Hence these is No Functional
or Operational
difference while working with either sendKeys(Keys.ENTER);
or WebElement.sendKeys(Keys.RETURN);
through Selenium.
Enter Key and Return Key
On computer keyboards, the Enter (or the Return on Mac OSX) in most cases causes a command line, window form, or dialog box to operate its default function. This is typically to finish an \"entry\" and begin the desired process, and is usually an alternative to pressing an OK button.
The Return is often also referred as the Enter and they usually perform identical functions; however in some particular applications (mainly page layout) Return operates specifically like the Carriage Return key from which it originates. In contrast, the Enter is commonly labelled with its name in plain text on generic PC keyboards.
References
- Enter Key
- Carriage Return
回答6:
selenium.keyPress(\"css=input.tagit-input.ui-autocomplete-input\", \"13\");
回答7:
You just do this:
final private WebElement input = driver.findElement(By.id(\"myId\"));
input.clear();
input.sendKeys(value); // the value we want to set to input
input.sendKeys(Keys.RETURN);
回答8:
For those folks who are using WebDriverJS Keys.RETURN
would be referenced as
webdriver.Key.RETURN
A more complete example as a reference might be helpful too:
var pressEnterToSend = function () {
var deferred = webdriver.promise.defer();
webdriver.findElement(webdriver.By.id(\'id-of-input-element\')).then(function (element) {
element.sendKeys(webdriver.Key.RETURN);
deferred.resolve();
});
return deferred.promise;
};
回答9:
driver.findElement(By.id(\"Value\")).sendKeys(Keys.RETURN);
or driver.findElement(By.id(\"Value\")).sendKeys(Keys.ENTER);
回答10:
I just like to note that I needed this for my Cucumber tests and found out that if you like to simulate pressing the enter/return key, you need to send the :return
value and not the :enter
value (see the values described here)
回答11:
For Selenium RC with Java:
selenium.keyPress(\"elementID\", \"\\13\");
For Selenium WebDriver(a.k.a. Selenium2) with java:
driver.findElement(By.id(\"elementID\")).sendKeys(Keys.ENTER);
OR,
driver.findElement(By.id(\"elementID\")).sendKeys(Keys.RETURN);
Another way to press Enter in WebDriver by using Actions class:
Actions action = new Actions(driver);
action.sendKeys(driver.findElement(By.id(\"elementID\")), Keys.ENTER).build().perform();
回答12:
search = browser.find_element_by_xpath(\"//*[@type=\'text\']\")
search.send_keys(u\'\\ue007\')
ENTER = u\'\\ue007\'
refer selenium documentation \'Special Keys\'
回答13:
if you are looking for \"how to press enter key from keyboard in selenium webdriver(java)\",then below code will definitely help you.
//assign key board object
Keyboard keyboard=((HasInputDevices) driver).getKeyboard();
//enter a key
keyboard.pressKey(Keys.ENTER);
回答14:
There are following ways of pressing keys - C#:
Driver.FindElement(By.Id(\"Value\")).SendKeys(Keys.Return);
OR
OpenQA.Selenium.Interactions.Actions action = new OpenQA.Selenium.Interactions.Actions(Driver);
action.SendKeys(OpenQA.Selenium.Keys.Escape);
OR
IWebElement body = GlobalDriver.FindElement(By.TagName(\"body\"));
body.SendKeys(Keys.Escape);
回答15:
Could be achieved using Action interface as well, in case of WebDriver -
WebElement username = driver.findElement(By.name(\"q\"));
username.sendKeys(searchKey);
Actions action = new Actions(driver);
action.sendKeys(Keys.RETURN);
action.perform();
回答16:
Try to use XPATH for searching the element and then, the following code works:
driver.findElement(By.xpath(\".//*[@id=\'txtFilterContentUnit\']\")).sendKeys(Keys.ENTER);
回答17:
For RUBY: -
driver.find_element(:id,\"XYZ\").send_keys:return
回答18:
You can try :
selenium.keyPress(\"id=\"\",\"\\\\13\");
回答19:
I had to send enter key in the middle of a text. So I passed the following text to send keys function to achieve 1\\n2\\n3
1\\N{U+E007}2\\N{U+E007}3
回答20:
For Selenium WebDriver using xpath (If the key is visible):
driver.findElement(By.xpath(\"xpath of text field\")).sendKeys(Keys.ENTER);
or,
driver.findElement(By.xpath(\"xpath of text field\")).sendKeys(Keys.RETURN);
回答21:
If you are in this specific situation:
a) want to JUST press the key, but not have a specific webElement to click on
b) you are using Selenium 2 (webDriver)
Then the solution is:
Actions builder = new Actions(webDriverInstance);
builder.sendKeys(Keys.RETURN).perform();
回答22:
JAVA/JavaScript:
You could probably do it this way also, non-natively:
public void triggerButtonOnEnterKeyInTextField(String textFieldId, String clickableButId)
{
((JavascriptExecutor) driver).executeScript(\"
elementId = arguments[0];
buttonId = arguments[1];
document.getElementById(elementId)
.addEventListener(\"keyup\", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById(buttonId).click();
}
});\", textFieldId, clickableButId);
}
回答23:
object.sendKeys(\"your message\",Keys.ENTER);
it works
回答24:
When You don\'t want to search any locator, You can use Robot class: eg:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
回答25:
Actions action = new Actions(driver);
action.sendKeys(Keys.RETURN);
回答26:
To enter keys using selenium First you need to import following library:
\"import org.openqa.selenium.Keys\"
then add this code where you want to enter key
\"WebElement.sendKeys(Keys.RETURN);\"
You can replace RETURN with any keys from the list according to your requirement