I'm looking for a quick way to type the 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. How can I do that with the Selenium type
command as there is no button to click?
You can simulate hit Enter key by adding "\n" to the entered text. For example textField. sendKeys("text you type into field" + "\n") .
In simple words, whatever is displayed as text on the browser will be returned as-is by the getText() method. If there is no text corresponding to a web element an empty string is returned.
When using it normally with a browser, enter key selects a single keyword and adds it to the list of keywords. With Selenium however enter key sends the form and return key selects a single keyword. You may use \n rather.
import org.openqa.selenium.Keys WebElement.sendKeys(Keys.RETURN);
The import
statement is for Java. For other languages, it is maybe different. For example, in Python it is from selenium.webdriver.common.keys import Keys
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With