简体   繁体   English

python selenium示例不起作用,没有名为Keys的模块说

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

I installed selenium via pip on a windows machine. 我通过pip在windows机器上安装了selenium。

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 : 来自http://code.google.com/p/selenium/issues/detail?id=1491

from selenium.webdriver.common.keys import Keys 来自selenium.webdriver.common.keys导入密钥

It seems your selenium installation is not in path, find out Selenium installation folder inside python installation. 看来你的selenium安装不在路径中,找到python安装里面的Selenium安装文件夹。 something like 就像是
C:\\Python33\\selenv\\Lib\\site-packages C:\\ Python33 \\ selenv \\ LIB \\站点包

or run it using command selenv\\Scripts\\python.exe yourseleniumscript.py from python installation folder. 或者从python安装文件夹中使用命令selenv \\ Scripts \\ python.exe yourseleniumscript.py运行它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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