【问题标题】:Trying to use Selenium 2 with Python bindings, but I'm getting an import error尝试将 Selenium 2 与 Python 绑定一起使用,但出现导入错误
【发布时间】:2021-06-23 16:33:23
【问题描述】:

我刚刚通过 pip install selenium 安装了 Selenium 2,并复制了一些示例测试以确保它正常工作:

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

driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.close()

我将它保存为 test.py 在我的 Mac 上我的主文件夹中的一个子文件夹中,但是当我运行 python test.py 时,我得到以下输出:

Traceback (most recent call last):
  File "demo.py", line 1, in <module>
    from selenium import webdriver
ImportError: cannot import name webdriver

如果我将该文件移动到我的主目录中,它就可以工作。如果你不知道,我刚刚开始使用 Selenium 和编程。对此的任何帮助将不胜感激。

【问题讨论】:

    标签: python webdriver selenium-webdriver


    【解决方案1】:

    听起来您的路径中有一些名为“selenium”的其他模块,python 正在尝试导入该模块,因为它在您的 python 路径中较早。例如,您是否将文件命名为“selenium.py”?

    要进行调试,请使用简单的import selenium 导入 selenium,然后打印使用 print selenium.__file__ 导入的文件的名称

    如果你有一个名为 "selenium.py" 的文件不是正确的 selenium 库,除了重命名或删除它之外,请确保你还删除了 "selenium.pyc",否则 python 将继续尝试从.pyc 文件。

    【讨论】:

    • 我得到的输出是:/Library/Python/2.7/site-packages/selenium/__init__.pyc 看起来正确吗?
    • 是的,看起来是正确的。是在你把它移到你的主目录之后还是之前?确保它位于您收到导入错误的位置,否则您只是在证明您已经知道的内容——您的主目录中的版本有效。
    • 谢谢你,当我在我(愚蠢地)名为 selenium.py 的文件中进行测试时帮助了我...... :)
    • 在我的情况下,我不小心将我的 py 文件命名为 selenium.py
    【解决方案2】:

    老问题,但我也做了同样的事情。将我的文件命名为“selenium.py”,它给出了这个错误消息。将文件重命名为其他名称,但仍然出现相同的错误。问题是 selenium.pyc 文件已经创建,因为我从终端运行了脚本。删除了 .pyc 文件,它运行起来就像一个魅力!

    【讨论】:

    • 我还必须关闭终端并打开一个新终端,因此更改生效
    • +1 当我错误地将我的文件命名为 selenium.py 时,我发现了这个问题 - 解决方法是按照描述删除 selenium.pyc 文件
    • 这需要更多的支持。在这里,我摸索了一个小时的路径和环境,一直以来都是因为我将文件命名为 selenium.py
    【解决方案3】:

    虽然这个问题似乎很长一段时间都处于不活跃状态,但我有同样的信息/类似的问题,以上答案都不适合。

    http://kevingann.blogspot.de/2012/11/troubleshooting-pydev-and-selenium.html 网站给出了关键提示。

    Selenium 出现了两次,一次在系统库中作为 egg,而“安装”版本在外部库中。打碎鸡蛋就行了。

    希望这对某人也有帮助

    【讨论】:

    • 是的,这有帮助,在 c:\Anaconda3\selenium 中有 selenium 文件夹,里面只有这些内容,一切正常,但 Pycharm 总是发出黄色警告,例如,“找不到参考”导入的模块 selenium.webdriver' 中的 PhantomJS:看起来它掩盖了这个:c:\Anaconda3\Lib\site-packages\selenium\ 删除 c:\Anaconda3\selenium 后,pycharm 中没有警告
    【解决方案4】:

    通过将 selenium 文件夹直接放在 Lib 而不是 site_packages 下解决了错误 ImportError: cannot import name webdriver or no module selenium2library

    【讨论】:

      【解决方案5】:

      将 selenium 目录从站点包复制到 lib 后,解决了 Pycharm 中的错误“在“导入的模块 selenium.webdriver”中找不到引用“Chrome”。 可以如上验证

      import selenium
      print (selenium.__file__)
      

      【讨论】:

        【解决方案6】:

        Sethe project interpreter as actual python.exe

        我能够成功运行下面的代码:

        from selenium import webdriver
        from selenium.webdriver.common.keys import Keys
        from selenium.webdriver.chrome.options import Options
        import time
        
        opts = Options()
        prefs = {"profile.managed_default_content_settings.images": 2}  
        opts.add_experimental_option("prefs", prefs)
        
        
        # enter complete path of chrome driver as argument to below line of code 
        browser = webdriver.Chrome('C:\\Users\\BLR153\\AppData\\Local\\Programs\\Python\\Python36-32\\selenium\\chromedriver.exe')
        # browser = webdriver.Firefox()
        
        browser.get('http://www.google.com')
        
        time.sleep(10)
        
        browser.quit()
        

        【讨论】:

          【解决方案7】:

          只需命名您的 pytnon 文件而不是 selenium.py !!! 它不是在 selenium 中寻找 webdriver,而是在你试图连接它的文件中寻找它

          【讨论】:

          • 在布莱恩的回答中:If you have a file named "selenium.py" which is not the proper selenium library, in addition to renaming or removing it, make sure you also delete "selenium.pyc", or python will continue to try to import from the .pyc file.
          【解决方案8】:
          1. 确保您只安装了一个python版本
          2. 安装 pip
          3. 使用 pip 安装 selenium
            pip install selenium
          4. 运行脚本

          希望对您有所帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-06
            • 1970-01-01
            • 2011-01-11
            • 1970-01-01
            相关资源
            最近更新 更多