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

标签 python selenium

我在 Windows 机器上通过 pip 安装了 selenium。

只需试用网站上的示例即可:

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

我不时更改为导入时间,它给出的错误消失了,现在我得到:

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

样本是否过时?

最佳答案

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

from selenium.webdriver.common.keys import Keys

关于python selenium 示例不起作用,说没有名为 Keys 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5503489/

相关文章:

python - 浮点精度仍应给出正确的结果

java - 使用 Jacoco 进行 Cucumber 测试的代码覆盖率

python - selenium.common.exceptions.WebDriverException : Message: Service chromedriver unexpectedly exited

python - 如何将函数或表达式作为参数传递?

python - 将字符串相乘,使其垂直向下移动一页?

python - Google App Engine 中的自引用 ReferenceProperty

java - Selenium -org.openqa.selenium.WebDriverException : b is null

ruby - 如何对 Web 应用程序进行全栈集成测试

selenium - 用于单击按钮的正确语法

Python:识别系统的状态空间模型