如何安装 Geckodriver?

Posted

技术标签:

【中文标题】如何安装 Geckodriver?【英文标题】:How do I install 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] 系统找不到指定的文件

【问题讨论】:

这个链接可以帮你解决这个问题askubuntu.com/questions/851401/… 【参考方案1】:
    您可以下载geckodriver 解压 复制该 .exe 文件并将您的文件放入 python 父文件夹(例如,C:\Python34) 编写脚本。

它将成功执行。

【讨论】:

【参考方案2】:

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

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

例子:

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

【讨论】:

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

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

    pip安装webdrivermanager

    pip install webdrivermanager

    为 Firefox 和 Chrome 安装驱动程序

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

    或者只为 Firefox 安装驱动

    webdrivermanager firefox --linkpath /usr/local/bin

    或者只为 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

见:

https://github.com/SeleniumHQ/selenium/issues/2672 https://superuser.com/questions/124239/what-is-the-default-path-environment-variable-setting-on-fresh-install-of-window https://askubuntu.com/questions/27213/what-is-the-equivalent-to-the-windows-program-files-folder-where-do-things-g

特别是关于如何看到驱动程序的解释, 它可以放在哪里,以及如何修改 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

【讨论】:

windows 7上没有apt @Cesar 这似乎很有希望:learningaboutelectronics.com/Articles/…(我自己没试过,我在 Linux 发行版上)。 我明白,但用户要求使用 Windows 7 ^_^ 这实际上效果很好:***.com/a/61084589/320926 你应该直接从 github 上的 Mozilla 版本获取 geckodriver:github.com/mozilla/geckodriver/releases github.com/mozilla/geckodriver github.com/mozilla/geckodriver/releases/tag/v0.30.0【参考方案9】:

对于 Python 3 - Selenium plus webdriver for Firefox;

    打开命令行

    输入 Pip install -U Selenium(-U 会将其升级到最新的 Selenium 版本。) This example selenium is already installed

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

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

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

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

Copying the file path of the geckodriver

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

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

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

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

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

【讨论】:

【参考方案10】:

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

【讨论】:

以上是关于如何安装 Geckodriver?的主要内容,如果未能解决你的问题,请参考以下文章

text 如何在Ubuntu 16.04上安装GeckoDriver

如何将 geckodriver 放入 PATH? [复制]

GeckoDriver的安装和使用

GeckoDriver的安装和使用

安装firefox驱动geckodriver的验证

mac os 安装 geckodriver