I am trying to execute the below code. I exclusively tried to import Keys from webdriver, but it still does not work.
from selenium import webdriver
import selenium.webdriver.common.keys
driver = webdriver.Firefox()
page = driver.get("https://www.python.org/")
print (driver.title)
finder = driver.find_element_by_class_name("search-field")
finder.send_keys("Python Test")
finder.send_keys(Keys.RETURN)
Output:
Welcome to Python.org
Traceback (most recent call last):
File "C:/Users/Arvind/Desktop/Python Tests/selenium_tests.py", line 9, in
<module>
finder.send_keys(Keys.RETURN)
NameError: name 'Keys' is not defined
>>>
It's used to ID the different Keys on the keyboard. Ex: Keys. RETURN means your ENTER key in your keyboard. These keys can then be used by selenium for keyboard emulation or other stuff.
We can type Enter/Return key in Selenium. We shall use the sendKeys method and pass Keys. ENTER as an argument to the method. Also, we can use pass Keys.
We can perform key press of (CTRL+A) with Selenium Webdriver. There are multiple ways to do this. We can use the Keys. chord() method to simulate this keyboard action.
You need to have
from selenium.webdriver.common.keys import Keys
instead of
import selenium.webdriver.common.keys
.
Then your code would run fine.
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