简体   繁体   中英

python selenium example doesn't work, says no module named Keys

I installed selenium via pip on a windows machine.

Just tryout out the sample on the website:

http://pypi.python.org/pypi/selenium

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert browser.title == "Yahoo!"
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("selenium" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()

I changed from time to import time and the error it was giving for that went away, now I am getting:

Traceback (most recent call last):
 File "test.py", line 3, in module
from selenium.common.keys import Keys
ImportError: no module named Keys

Is the sample out of date?

From http://code.google.com/p/selenium/issues/detail?id=1491 :

from selenium.webdriver.common.keys import Keys

It seems your selenium installation is not in path, find out Selenium installation folder inside python installation. something like
C:\\Python33\\selenv\\Lib\\site-packages

or run it using command selenv\\Scripts\\python.exe yourseleniumscript.py from python installation folder.

将C:\\ Python27 \\ Lib \\ site-packages \\ selenium \\ webdriver \\ common \\ keys.py复制到C:\\ Python27 \\ Lib \\ site-packages \\ selenium \\ common

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM