13

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
>>>

1 Answer 1

34

You need to have

from selenium.webdriver.common.keys import Keys instead of

import selenium.webdriver.common.keys.

Then your code would run fine.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.