Need help importing Keys from selenium.webdriver.c

2020-07-15 02:54发布

问题:

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:

You need to have this

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

import selenium.webdriver.common.keys

and then your code would run fine.