【问题标题】:AttributeError Issue: module 'selenium.webdriver' has no attribute 'Chrome'AttributeError 问题:模块“selenium.webdriver”没有属性“Chrome”
【发布时间】:2018-10-11 07:11:31
【问题描述】:

我刚刚使用 Python 启动了 Selenium。而且我正面临属性错误问题。

  • 已安装 Python 3.6.5 并安装了最新的 selenium 包(selenium-3.11.0)

  • 还在环境中添加了脚本和 Python 文件夹路径 变量:PATH

  • 下载了 chromedriver.exe 并添加了 相应的文件路径到环境变量中。

但是在运行下面的代码时:

from selenium import webdriver
driver = webdriver.Chrome("E:\Selenium\chromedriver_win32\chromedriver.exe")

它抛出以下错误:

C:\Users\Sooraj\venv\firstpgm\Scripts\python.exe C:/Users/Sooraj/PycharmProjects/Selenium/First.py
Traceback (most recent call last):
  File "C:/Users/Sooraj/PycharmProjects/Selenium/First.py", line 2, in 
<module>
driver=webdriver.Chrome("E:\Selenium\chromedriver_win32\chromedriver.exe")
AttributeError: module 'selenium.webdriver' has no attribute 'Chrome'

Process finished with exit code 1

尝试了 Stack Overflow 中提供的所有其他解决方案,例如卸载和重新安装 Python 以及升级 selenium。但没有任何帮助。

代码使用 PyCharm IDE 运行,但使用 IDLE 运行时运行良好。

可以在 Sitepackages -> selenium -> webdriver 下找到 firefox、chrome、safari、phantomjs、android 等文件夹。但不知道为什么它仍然显示“Webdriver has no attribute chrome”

附上以上截图。在 webdriver 下显示没有模块 chrome()

任何帮助将不胜感激。

【问题讨论】:

标签: python selenium selenium-webdriver


【解决方案1】:
from selenium import webdriver

driver = webdriver.Chrome()

这是编写该代码的正确方法,如果您想使用 firefox 或其他东西,则将 chrome 更改为 firefox ...还请阅读第一个文档并查找一些示例,如果您找到,请将其放在这里什么都没有

也可以使用 pip install selenium !

【讨论】:

    【解决方案2】:

    您可以删除您最近创建的文件。当我来到这个线程时,我面临同样的问题,但对我没有任何帮助。我刚刚删除了我最近的文件,一切正常:-)

    在创建新的 python 文件之前 selenium 工作正常。当我在同一文件夹中创建新的 python 文件时,它开始显示此错误。当我删除文件时,一切正常。

    【讨论】:

    • 这只是发生在我身上。为什么在同一目录中添加 .py 文件会破坏 selenium?
    【解决方案3】:

    你应该这样写:browser = webdriver.Chrome(executable_path=r"chromedriver.exe"),并请确保你的系统上安装了谷歌浏览器。

    【讨论】:

    • 我试过你的意见。 chromedriver.exe 已下载。我已经在环境变量中添加了路径。但是还是不行
    【解决方案4】:

    尝试使用以下命令

    driver = webdriver.chrome.webdriver.WebDriver(executable_path='E:\Selenium\chromedriver_win32\chromedriver.exe')
    

    参考:官方文档https://seleniumhq.github.io/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html#selenium.webdriver.chrome.webdriver.WebDriver

    【讨论】:

    • 我试过你的输入。但是错误仍然存​​在......我应该降级我的python版本吗?
    【解决方案5】:

    首先在 URL chrome://version/ 中使用它来检查系统的 google chrome 版本

    然后根据chrome版本从下面提到的网站下载chrome驱动程序 https://chromedriver.chromium.org/downloads

    然后在 pycharm 或 sublime 文本中输入这个

    import selenium
    
    from selenium import webdriver
    
    driver = webdriver.chrome.webdriver.WebDriver(executable_path='C:/drivers/chromedriver_win32 (1)/chromedriver.exe')
    
    driver.get("http://www.python.org")
    

    然后运行,它肯定有效

    【讨论】:

      【解决方案6】:

      Windows 7:Pycharm IDE 版本 2018.1.2 导航到

      项目名称->venv

      打开 pyenv.cfg

      改变

      include-system-site-packages = false
      

      include-system-site-packages = true
      

      【讨论】:

        【解决方案7】:

        请检查您的文件名,它不是特殊名称。例如 selectors.py

        【讨论】:

        • 欢迎来到 Stack Overflow。请注意,这个问题是两年前的问题。随着声望的增加,您将可以离开 cmets,但请不要使用回答功能离开 cmets。
        【解决方案8】:

        我遇到了同样的问题,我们解决了!!我安装了最新版本的python 3.9。 我运行它,它对我大喊要硒。 所以我进入命令提示符并做了:

        pip install selenium
        

        它安装了硒。我运行了它,它成功了。

        【讨论】:

          猜你喜欢
          • 2018-11-26
          • 1970-01-01
          • 2018-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-14
          • 2019-02-18
          相关资源
          最近更新 更多