【问题标题】:Python: What modules do I need to include in the directory of my program to run Selenium with Unittest?Python:我需要在我的程序目录中包含哪些模块才能使用 Unittest 运行 Selenium?
【发布时间】:2014-09-17 01:12:29
【问题描述】:

我正在尝试使用 Pycharm IDE 运行以下代码:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("selenium")
        elem.send_keys(Keys.RETURN)

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

执行此操作时出现以下错误:

Traceback (most recent call last):
File "/Users/BigRed/PycharmProjects/python 3/source.py", line 8, in setUp
self.driver = webdriver.Firefox()
AttributeError: 'module' object has no attribute 'Firefox'

在源文件的目录下,我还有 selenium 文件夹,里面有 webdriver 文件,还有exceptions.pyselenium.py。有什么我想念的吗?还是问题完全不同?

感谢您的帮助。

【问题讨论】:

  • 请检查webdriver.py 是否有class Firefox
  • 它没有,不。这就是为什么我想知道我还需要什么其他文件。我希望有硒经验的人可以为我指明正确的方向,因为我是新手
  • 源码在git中有文件selenium/webdriver/firefox/webdriver.py。检查网址code.google.com/p/selenium/source/browse/py/selenium/webdriver/…
  • 我的 webdriver.py 和那个一样
  • 那么它必须工作:(。请检查sys.path是否有那个目录。

标签: python google-chrome selenium browser webdriver


【解决方案1】:

我也遇到了同样的问题,我确实喜欢这个

1) open terminal (ctrl+alt+t)
2) check $ python --version
3) now install firefox using pip or sudo apt-get

1) open terminal (ctrl+alt+t)
2) check $ python --version                  (2.7.5....) and 
3) In pycharm go to project interpreter and select the same python version as project interpreter.

4) 现在运行就可以了。

【讨论】:

    猜你喜欢
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 2012-10-26
    • 2017-11-04
    • 2010-10-18
    • 2012-09-08
    相关资源
    最近更新 更多