【问题标题】:How do I install Geckodriver?如何安装 Geckodriver?
【发布时间】:2017-05-02 15:20:52
【问题描述】:

我正在尝试在 Python 中使用 Selenium。但是,鉴于https://pypi.python.org/pypi/selenium的以下内容,我不知道该怎么做

Selenium 需要驱动程序才能与所选浏览器交互。例如,Firefox 需要 geckodriver,在运行以下示例之前需要安装它。确保它在您的 PATH 中,例如,将其放在 /usr/bin or /usr/local/bin

我正在运行 Windows 7 32 位。我在这里找到了 geckodriver:https://github.com/mozilla/geckodriver/releases

我主要使用 Python 的 Anaconda 发行版来处理 excel,所以我不知道什么是“PATH”

谢谢,

更新

我更新了 PATH,如 cmets 中所示。这是完整的错误回溯。

Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\user1>python

Python 3.5.2 |Anaconda 4.2.0(32 位)| (默认,2016 年 7 月 5 日,11:45:57)[MSC v.1 900 32 位 (Intel)] 在 win32 上

输入“帮助”、“版权”、“信用”或“许可”以了解更多信息。

from selenium import webdriver
driver = webdriver.Firefox()

Traceback(最近一次通话最后一次):

文件“”,第 1 行,在

文件“C:\Users\user1\AppData\Local\Continuum\Anaconda3\lib\site -packages\selenium-2.53.6-py3.5.egg\selenium\webdriver\firefox\webdriver.py", li ne 80, 在 init self.binary,超时)

文件“C:\Users\user1\AppData\Local\Continuum\Anaconda3\lib\site -packages\selenium-2.53.6-py3.5.egg\selenium\webdriver\firefox\extension_connect ion.py",第 52 行,在 init 中 self.binary.launch_browser(self.profile, timeout=timeout)

文件“C:\Users\user1\AppData\Local\Continuum\Anaconda3\lib\site -packages\selenium-2.53.6-py3.5.egg\selenium\webdriver\firefox\firefox_binary.py ",第 67 行,在 launch_browser 中 self._start_from_profile_path(self.profile.path)

文件“C:\Users\user1\AppData\Local\Continuum\Anaconda3\lib\site -packages\selenium-2.53.6-py3.5.egg\selenium\webdriver\firefox\firefox_binary.py ",第 90 行,在 _start_from_profile_path 中 env=self._firefox_env)

文件“C:\Users\user1\AppData\Local\Continuum\Anaconda3\lib\subp rocess.py",第 947 行,在 init 中 restore_signals, start_new_session)

文件“C:\Users\user1\AppData\Local\Continuum\Anaconda3\lib\subp rocess.py”,第 1224 行,在 _execute_child 中 启动信息)

FileNotFoundError: [WinError 2] 系统找不到指定的文件

【问题讨论】:

标签: python selenium path webautomation geckodriver


【解决方案1】:
  1. 您可以下载geckodriver
  2. 解压
  3. 复制该 .exe 文件并将您的文件放入 python 父文件夹(例如,C:\Python34
  4. 编写脚本。

它将成功执行。

【讨论】:

    【解决方案2】:

    如果您在 Windows 上,最简单的方法:

    driver = webdriver.Firefox(executable_path=r'[Your path]\geckodriver.exe')
    

    例子:

    driver = webdriver.Firefox(executable_path=r'D:\geckodriver.exe')
    

    【讨论】:

    • 我将它添加到 PATH 中,作为管理员,我与用户共享了该文件夹,实际上是与我共享了一个赞成票。
    【解决方案3】:

    有一种安装 Geckodriver 的简单方法:

    1. pip安装webdrivermanager

      pip install webdrivermanager

    2. 为 Firefox 和 Chrome 安装驱动程序

      webdrivermanager firefox chrome --linkpath /usr/local/bin

    3. 或者只为 Firefox 安装驱动

      webdrivermanager firefox --linkpath /usr/local/bin

    4. 或者只为 Chrome 安装驱动

      webdrivermanager chrome --linkpath /usr/local/bin

    【讨论】:

    • 导入from webdrivermanager import GeckoDriverManager时,谁以编程方式获取可执行路径?
    【解决方案4】:

    如果您使用的是 macOS/Apple,则可以使用 Homebrew: brew install geckodriver

    看到这个相关的question

    【讨论】:

      【解决方案5】:

      一些选项,选择1:

      • 将 exe 文件移动到 PATH 环境变量中的文件夹中。
      • 更新PATH 以获得包含该exe 的目录。
      • 显式覆盖os.environ["webdriver.gecko.driver"]

      基本上将 geckodriver 拖放到您拥有可执行文件的地方,然后您应该能够打开命令行并使用它。

      /bin 在 Linux 上,C:\Program Files

      见:

      特别是关于如何看到驱动程序的解释, 它可以放在哪里,以及如何修改 selenium 找到它的方式。

      【讨论】:

      • 好的 - 我通过环境变量将 geckodriver 添加到 PATH 中。 C:\Users\User1\AppData\Local\Continuum\Anaconda3;C:\Users\User1\AppData\Local\Continuum\Anaconda3\Scripts;C:\Users\User1\AppData\Local\Continuum\Anaconda3\Library\bin ;C:\Users\User1\Documents\Python Scripts\Python Modules\geckodriver\geckodriver.exe
      • running from selenium import webdriver driver = webdriver.Firefox() 导致新错误:FileNotFoundError: [WinError 2] The system cannot find the file specified @jmunsch
      • cgoldberg: PATH environment variable contains a list of directories to scan. You have added a path containing a filename, not a directory.
      • @polonius11 差不多了
      • 我应该改变什么? C:\Users\User‌​1\Documents\Python Scripts\Python Modules\geckodriver\geckodriver.exe .... 这个路径有 geckodriver.exe 文件名吗? .....@jmunsch?我以前没有使用过目录。
      【解决方案6】:

      对我来说这很有效(Windows 10、Firefox 浏览器):

      from selenium import webdriver
      driver = webdriver.Firefox(executable_path=r'C:\......YOUR_PATH.......\geckodriver.exe')
      driver.get('http://EXAMPLE_URL.com')
      

      【讨论】:

        【解决方案7】:

        同时对于Win10你可以简单使用

        from selenium import webdriver
        from webdriver_manager.firefox import GeckoDriverManager
        
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
        driver.get("https://www.google.com")
        

        这将在第一次使用之前下载 geckodrive 并将其存储在适当的位置。无需明确设置任何路径。

        【讨论】:

          【解决方案8】:

          对于 Linux:

          以下简单的安装对我有用:

          sudo apt install firefox-geckodriver
          

          无需额外安装驱动程序。

          参考:https://github.com/timgrossmann/InstaPy/issues/5282#issuecomment-666283451

          对于窗户:

          按照此处的说明进行操作:http://www.learningaboutelectronics.com/Articles/How-to-install-geckodriver-Python-windows.php

          【讨论】:

          【解决方案9】:

          对于 Python 3 - Selenium plus webdriver for Firefox;

          1. 打开命令行
          2. 输入 Pip install -U Selenium(-U 会将其升级到最新的 Selenium 版本。) This example selenium is already installed

          3. 转到https://github.com/mozilla/geckodriver/releases

          4. 在撰写本文时,我选择了最新版本,即页面顶部列出的版本。对我来说是 v0.24.0。

          5. 向下滚动到资产,然后单击并下载正确的驱动程序。对于 Windows,它将是一个 zip 文件。最有可能是64位。 Download the webdriver by clicking on the link 5.右键单击下载的文件并解压缩文件。

          6. 将文件复制并粘贴到 python 目录中的某个位置。例如如果我在 C:\Python\Python37 中安装 Python,我会将文件粘贴到那里,这样 gecko 将位于 C:\Python\Python37\geckodriver-v0.24.0-win64

          Copying the file path of the geckodriver

          1. 在您刚刚复制的文件夹中将是 geckodriver.exe

          2. 在 Windows 10 中,单击“windows”按钮并搜索“环境变量” Find environment variables 或使用这些说明找到它; https://www.computerhope.com/issues/ch000549.htm

          3. 单击右下角的“环境变量”框。

          4. 在底部的“系统变量”框中突出显示“路径”变量,如下所示 Adding environment variable Path

          5. 按编辑,然后在列表底部添加条目。复制并粘贴 geckodriver.exe 文件所在的位置。对我来说,它是 C:\Python\Python37\geckodriver-v0.24.0-win64 (或者您在步骤 6 中复制文件的位置) Adding gecko to the windows PATH

          【讨论】:

            【解决方案10】:

            为避免链接过时,请参考来源。 https://github.com/mozilla/geckodriver 按照“下载”>“发布”链接的自述说明进行操作。

            【讨论】:

              猜你喜欢
              • 2017-07-20
              • 2021-11-02
              • 2019-07-12
              • 1970-01-01
              • 1970-01-01
              • 2018-04-30
              • 1970-01-01
              • 2022-10-25
              相关资源
              最近更新 更多