【问题标题】:Selenium using Python - Geckodriver executable needs to be in PATHSelenium 使用 Python - Geckodriver 可执行文件需要在 PATH 中
【发布时间】:2017-03-05 14:51:08
【问题描述】:

我是编程新手,大约两个月前开始使用 Python,现在正在阅读 Sweigart 的 Automate the Boring Stuff with Python 文本。我正在使用IDLE,并且已经安装了 Selenium 模块和 Firefox 浏览器。

每当我尝试运行 webdriver 函数时,我都会得到:

from selenium import webdriver
browser = webdriver.Firefox()

例外:

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0DA1080>>
Traceback (most recent call last):
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
    self.stop()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x00000249C0E08128>>
Traceback (most recent call last):
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
    self.stop()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Traceback (most recent call last):
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:\Python\Python35\lib\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:\Python\Python35\lib\subprocess.py", line 1224, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    browser = webdriver.Firefox()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
    self.service.start()
  File "C:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

我想我需要为geckodriver 设置路径,但我不知道该怎么做,我该怎么做呢?

【问题讨论】:

  • 请看我对类似问题的回答here
  • 我将 geckodriver.exe 放在 Python/Python35 目录中,因此它具有相同的路径,但我遇到了更多问题。
  • 在 Mac 上:brew install geckodriver
  • 我发现通过 Chrome 浏览器运行它比在 Firefox 上运行它要快一点,您只需为此下载chromedriver
  • 注意:Testcafe 最近开源了。它不需要任何浏览器插件,它们是内置的。我想使用 Selenium,但这看起来是一个有趣的选择。

标签: python selenium firefox selenium-firefoxdriver geckodriver


【解决方案1】:

selenium.common.exceptions.WebDriverException:消息:“geckodriver”可执行文件需要在 PATH 中。

First of all you will need to download latest executable geckodriver from here to run latest Firefox using Selenium

实际上,Selenium 客户端绑定尝试从系统PATH 中定位geckodriver 可执行文件。您需要将包含可执行文件的目录添加到系统路径。

  • 在 Unix 系统上,如果您使用的是与 Bash 兼容的 shell,您可以执行以下操作将其附加到系统的搜索路径:

      export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
    
  • 在 Windows 上,您需要更新 Path 系统变量以将完整目录路径添加到可执行的 geckodriver manuallycommand line**(不要忘记重启系统后将可执行的 geckodriver 添加到系统 PATH 中以生效)**。原理和Unix一样。

现在你可以像下面一样运行你的代码了:-

from selenium import webdriver

browser = webdriver.Firefox()

selenium.common.exceptions.WebDriverException: 消息:预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有提供“moz:firefoxOptions.binary”功能,命令行上没有设置二进制标志

异常清楚地表明您在 Selenium 尝试查找 Firefox 并从默认位置启动时已将 Firefox 安装在其他位置,但找不到它。您需要提供明确的 Firefox 安装二进制位置来启动 Firefox,如下所示:-

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
browser = webdriver.Firefox(firefox_binary=binary)

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

对于 Windows:

从 GitHub 下载文件,解压并粘贴到 Python 文件中。它对我有用。

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

对我来说,我的路径路径是:

C:\Users\MYUSERNAME\AppData\Local\Programs\Python\Python39

【讨论】:

  • 谢谢,但我在C:\Python\Python35\selenium 目录上设置了我的geckodriver.exe,并设置了您描述的路径,但它给了我以下错误:
  • 谢谢@Saurabh Gaur,它现在可以工作了。我手动将 Firefox 的路径添加到系统变量中,一切正常。启动需要一点时间,但我猜这很正常。谢谢!
  • 我在开始指定 firefox 二进制路径时收到错误“WebDriverException:消息:无法启动浏览器:权限被拒绝”,但重新启动计算机(Windows 10)解决了问题。 - 以防万一其他人遇到和我一样的问题。
  • 什么是二进制文件?这是否意味着可执行?
  • 除了这个答案,我想扩展在 unix 环境中设置PATH。您可以在代码中设置它,因为您在系统范围内不需要它:os.environ["PATH"] += os.pathsep + 'path/to/dir/containing/geckodriver/' 或者只是将 geckodriver 二进制文件保存在路径中已经存在的目录中:mv geckodriver /usr/local/bin
【解决方案2】:

这为我解决了。

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get('http://inventwithpython.com')

【讨论】:

  • 如果您获得错误的权限,请尝试从路径中删除 [r'] 只是 "excecutable_path='path\to\your'diretory'"
  • @adityarawat 你在什么操作系统上?
  • 但现在我得到的是 OSError 而不是错误的权限。我以某种方式设法将 geckodriver 复制到 /usr/local/bin。但现在这个新错误正在杀死我
  • @adityarawat 你可以从这里独立下载 geckodriver:github.com/mozilla/geckodriver/releases 用 tar 解压它并用 chmod +x 使其可执行,它不必在 /usr/local/bin 中,你只需要指定你的路径
  • 为了清楚我已经下载了 arm7hf.tar 文件并将其解压缩并使用命令 export PATH=$PATH:geckodriver 将其添加到路径中(它在桌面中解压缩)。但这也无济于事。我仍然得到 OSError[errno 8 ]
【解决方案3】:

这些步骤解决了我在 Ubuntu 和 Firefox 50 上的问题。

  1. 下载geckodriver

  2. 将 geckodriver 复制到文件夹/usr/local/bin

你确实不需要需要添加:

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/bin/firefox'
browser = webdriver.Firefox(capabilities=firefox_capabilities)

【讨论】:

  • 在你的代码中你不能添加能力变量
  • 你知道如何设置火狐的下载目录吗?我添加了以下问题Set Firefox Preferences。任何帮助将不胜感激。
  • 在 Debian 或 Ubuntu 中,您必须使用 apt 命令安装 Firefox。对于 Windows,我不知道抱歉
  • 谢谢。应用此答案后,我进一步采用此解决方案来处理后续问题:stackoverflow.com/questions/43713445/…
  • 谢谢,Pycharm 没有找到 geckodriver,虽然它在 home 和项目文件夹本身中,但是在将它移动到 /usr/local/bin 后它工作得很好
【解决方案4】:

我看到讨论仍然在谈论通过下载二进制文件并手动配置路径来设置 geckodriver 的旧方法。

这可以使用webdriver-manager自动完成

pip install webdriver-manager

现在问题中的上述代码将与以下更改一起工作,

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

【讨论】:

  • Safari 甚至 Internet Explorer 是否有类似的驱动程序管理器?
  • 或者使用pip3?
  • 它有点工作,但我认为geckodriver 需要在路径中。临时的,比如:export PATH=$PATH:/home/embo/.wdm/drivers/geckodriver/linux64/v0.28.0
  • 太棒了!这太容易了!
  • 非常感谢。这是唯一有效的方法。我正在运行一个 cron 作业,该作业将一个程序生成到一个运行 selenium 的线程。
【解决方案5】:

@saurabh 的回答解决了这个问题,但它没有解释为什么 Automate the Boring Stuff with Python 不包括这些步骤。

这是因为本书基于 Selenium 2.x 并且该系列的 Firefox 驱动程序不需要 Gecko 驱动程序。在 Selenium 开发时,驱动浏览器的 Gecko 接口不可用。

Selenium 2.x 系列中的 latest version 是 2.53.6(参见例如 [这些答案][2],以便更轻松地查看版本)。

[2.53.6 版本页面][3] 根本没有提到 Gecko。但从 3.0.2 版开始,文档 [明确指出][4] 您需要安装 Gecko 驱动程序。

如果在升级(或安装在新系统上)之后,之前(或在旧系统上)运行良好的软件不再运行,并且您很着急,请通过以下操作将 Selenium 版本固定在您的 virtualenv 中

pip install selenium==2.53.6

当然,开发的长期解决方案是使用最新版本的 selenium 设置新的 virtualenv,安装 Gecko 驱动程序并测试一切是否仍按预期工作。

但是主要版本的升级可能会引入您的书未涵盖的其他 API 更改,因此您可能希望坚持使用旧版 Selenium,直到您有足够的信心可以修复 Selenium 2 和 Selenium 3 之间的任何差异自己 API。

[2]:https://stackoverflow.com/a/40746017/1307905) [3]:https://pypi.python.org/pypi/selenium/2.53.6 [4]:https://pypi.python.org/pypi/selenium#drivers

【讨论】:

    【解决方案6】:

    在已安装 Homebrew 的 macOS 上,您可以简单地运行终端命令

    $ brew install geckodriver
    

    因为自制软件已经扩展了PATH,所以不需要修改任何启动脚本。

    【讨论】:

    • @roskakori 我这样做了,它安装成功,但我仍然遇到同样的错误
    • 这是为我做的,谢谢。使用 Firefox 72.0.2 和 Selenium 3.141.0
    • 这对我也有用,不需要修改任何东西。谢谢!
    【解决方案7】:

    为 Selenium Python 设置 geckodriver:

    需要用FirefoxDriver设置geckodriver路径,如下代码:

    self.driver = webdriver.Firefox(executable_path = 'D:\Selenium_RiponAlWasim\geckodriver-v0.18.0-win64\geckodriver.exe')
    

    为您的合适操作系统下载 geckodriver(来自https://github.com/mozilla/geckodriver/releases)→ 将其解压缩到您选择的文件夹中→ 如上所述正确设置路径。

    我在 Windows 10 上使用 Python 3.6.2 和 Selenium WebDriver 3.4.3。

    另一种设置 geckodriver 的方法:

    i) 只需将 geckodriver.exe 粘贴到 /Python/Scripts/ 下(在我的情况下,文件夹是:C:\Python36\Scripts
    ii) 现在编写如下简单代码:

    self.driver = webdriver.Firefox()
    

    【讨论】:

      【解决方案8】:

      如果您使用Anaconda,您只需激活您的虚拟环境,然后使用以下command 安装geckodriver

          conda install -c conda-forge geckodriver
      

      【讨论】:

      • 是的,它安装了 geckodriver,但 pycharm 错误保持不变。
      【解决方案9】:

      Ubuntu 18.04+ 和最新版本的 geckodriver

      这也应该适用于其他 *nix 变体。

      export GV=v0.30.0
      wget "https://github.com/mozilla/geckodriver/releases/download/$GV/geckodriver-$GV-linux64.tar.gz"
      tar xvzf geckodriver-$GV-linux64.tar.gz
      chmod +x geckodriver
      sudo cp geckodriver /usr/local/bin/
      

      对于 Mac 更新至:

      geckodriver-$GV-macos.tar.gz
      

      【讨论】:

        【解决方案10】:

        Windows 最简单的方法!

        here 下载最新版本的geckodriver。将 geckodriver.exe 文件添加到 Python 目录(或已在 PATH 中的任何其他目录)。这应该可以解决问题(已在 Windows 10 上进行了测试)。

        【讨论】:

        • Windows Server 2019 - 在系统中添加 geckodriver.exe 的路径后它不起作用,但在将 geckoriver.exe 复制到 python 路径后它起作用了!谢谢:)
        【解决方案11】:

        Mac 步骤

        简单的解决方案是下载 GeckoDriver 并将其添加到您的系统路径中。您可以使用以下两种方法之一:

        短方法

        1. 下载并解压Geckodriver

        2. 在启动驱动时提及路径:

           driver = webdriver.Firefox(executable_path='/your/path/to/geckodriver')
          

        长方法

        1. 下载并解压Geckodriver

        2. 打开.bash_profile。如果您还没有创建它,您可以使用命令:touch ~/.bash_profile。然后使用打开它:open ~/.bash_profile

        3. 考虑到 GeckoDriver 文件存在于您的下载文件夹中,您可以将以下行添加到 .bash_profile 文件中:

           PATH="/Users/<your-name>/Downloads/geckodriver:$PATH"
           export PATH
          

        通过这种方式,您将 GeckoDriver 的路径附加到您的系统路径中。这会在执行 Selenium 脚本时告诉系统 GeckoDriver 所在的位置。

        1. 保存.bash_profile 并强制执行。这会立即加载值,而无需重新启动。为此,您可以运行以下命令:

        source ~/.bash_profile

        1. 就是这样。你完成了!您现在可以运行 Python 脚本了。

        【讨论】:

        • 我能够使用 Homebrew 下载 geckodriverbrew install geckodriver,然后通过:driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver") 启动 Firefox
        【解决方案12】:

        为该线程的未来读者提供一些额外的输入/说明:

        以下足以作为 Windows 7、Python 3.6 和 Selenium 3.11 的解决方案:

        dsalaj's note 对于 Unix 的另一个答案也适用于 Windows;可以避免在 Windows 级别修改 PATH 环境变量和重新启动 Windows 系统。

        (1) 下载 geckodriver(如本线程前面所述)并将(解压缩的)geckdriver.exe 放在 X:\Folder\of\your\choice

        (2) Python代码示例:

        import os;
        os.environ["PATH"] += os.pathsep + r'X:\Folder\of\your\choice';
        
        from selenium import webdriver;
        browser = webdriver.Firefox();
        browser.get('http://localhost:8000')
        assert 'Django' in browser.title
        

        注意事项: (1) 上述代码打开指定URL的火狐浏览器可能需要10秒左右。 (2) 如果没有服务器已经在指定的 URL 上运行或提供标题包含字符串 'Django' 的页面,Python 控制台将显示以下错误: selenium.common.exceptions.WebDriverException:消息:到达错误页面:about:neterror?e=connectionFailure&u=http%3A//localhost%3A8000/&c=UTF-8&f=regular&d=Firefox%20can%E2%80%9

        【讨论】:

          【解决方案13】:

          geckodriver默认不安装。

          $ geckodriver
          
          Command 'geckodriver' not found, but it can be installed with:
          
          sudo apt install firefox-geckodriver
          
          $
          

          以下命令不仅会安装它,还会将其放入可执行文件PATH

          sudo apt install firefox-geckodriver
          

          只需一步即可解决问题。我遇到了和你一样的错误,安装后它就消失了。来试试吧。

          $ which geckodriver
          /usr/bin/geckodriver
          $
          $ geckodriver
          1337    geckodriver    INFO    Listening on 127.0.0.1:4444
          ^C
          

          【讨论】:

          • 这对我有用。它还消除了下载驱动程序并将其放入正确路径的痛苦。
          • 这是真正的答案。
          【解决方案14】:

          我实际上发现您可以使用最新的 geckodriver,而无需将其放入系统路径中。目前我正在使用

          https://github.com/mozilla/geckodriver/releases/download/v0.12.0/geckodriver-v0.12.0-win64.zip

          火狐 50.1.0

          Python 3.5.2

          硒 3.0.2

          Windows 10

          我正在运行一个 VirtualEnv(我使用 PyCharm 管理它,我假设它使用 Pip 来安装所有东西)。

          在下面的代码中,我可以使用 executable_path 参数为 geckodriver 使用特定路径(我通过查看 Lib\site-packages\selenium\webdriver\firefox\webdriver.py)。请注意,我怀疑调用 webdriver 时参数参数的顺序很重要,这就是为什么 executable_path 在我的代码中位于最后(最右边的倒数第二行)。

          您可能还注意到,如果您正在测试的站点具有不受信任的证书,我会使用自定义 Firefox 配置文件来解决您将遇到的 sec_error_unknown_issuer 问题。见How to disable Firefox's untrusted connection warning using Selenium?

          经过调查,发现 Marionette 驱动程序不完整且仍在进行中,并且设置各种功能或配置文件选项以取消或设置证书都不起作用。所以使用自定义配置文件更容易。

          不管怎样,下面是我如何让 geckodriver 在不进入路径的情况下工作的代码:

          from selenium import webdriver
          from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
          
          firefox_capabilities = DesiredCapabilities.FIREFOX
          firefox_capabilities['marionette'] = True
          
          #you probably don't need the next 3 lines they don't seem to work anyway
          firefox_capabilities['handleAlerts'] = True
          firefox_capabilities['acceptSslCerts'] = True
          firefox_capabilities['acceptInsecureCerts'] = True
          
          # In the next line I'm using a specific Firefox profile because
          # I wanted to get around the sec_error_unknown_issuer problems with the new Firefox and Marionette driver
          # I create a Firefox profile where I had already made an exception for the site I'm testing
          # see https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles#w_starting-the-profile-manager
          
          ffProfilePath = 'D:\Work\PyTestFramework\FirefoxSeleniumProfile'
          profile = webdriver.FirefoxProfile(profile_directory=ffProfilePath)
          geckoPath = 'D:\Work\PyTestFramework\geckodriver.exe'
          browser = webdriver.Firefox(firefox_profile=profile, capabilities=firefox_capabilities, executable_path=geckoPath)
          browser.get('http://stackoverflow.com')
          

          【讨论】:

          • 我得到了 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes... 我必须将 gecko 路径更改为 epath = r'C:\Users\step_\Documents\mtg_buyer \geckodrivers\geckodriver.exe'。可能是因为我用的是中文Windows 10?
          【解决方案15】:

          对于 Ubuntu 16.04+ 版本,您可以执行以下操作:-

          对于火狐:-
          sudo apt-get install firefox-geckodriver

          对于 Chrome:-
          sudo apt-get install chromium-chromedriver

          【讨论】:

          • 解决了我的 Firefox 问题。
          【解决方案16】:

          考虑安装一个容器化的 Firefox:

          docker pull selenium/standalone-firefox
          docker run --rm -d -p 5555:4444 --shm-size=2g selenium/standalone-firefox
          

          使用webdriver.Remote连接:

          driver = webdriver.Remote('http://localhost:5555/wd/hub', DesiredCapabilities.FIREFOX)
          driver.set_window_size(1280, 1024)
          driver.get('https://toolbox.googleapps.com/apps/browserinfo/')
          driver.save_screenshot('info.png')
          

          【讨论】:

            【解决方案17】:

            非常遗憾的是,没有一本关于 Selenium/Python 的书籍以及大多数通过 Google 发表的关于这个问题的 cmets 都没有清楚地解释在 Mac 上设置它的路径逻辑(一切都是 Windows !!!!!!)。 YouTube 视频都是在你设置好路径后“获取”的(在我看来,这是便宜的出路!)。因此,对于您出色的 Mac 用户,请使用以下命令来编辑您的 Bash 路径文件:

            touch ~/.bash_profile; open ~/.bash_profile*
            

            然后添加类似这样的路径....

            # Setting PATH for geckodriver
            PATH=“/usr/bin/geckodriver:${PATH}”
            export PATH
            
            # Setting PATH for Selenium Firefox
            PATH=“~/Users/yourNamePATH/VEnvPythonInterpreter/lib/python2.7/site-packages/selenium/webdriver/firefox/:${PATH}”
            export PATH
            
            # Setting PATH for executable on Firefox driver
            PATH=“/Users/yournamePATH/VEnvPythonInterpreter/lib/python2.7/site-packages/selenium/webdriver/common/service.py:${PATH}”
            export PATH*
            

            这对我有用。我担心的是 Selenium Windows 社区什么时候开始玩真正的游戏,并将我们 Mac 用户纳入他们傲慢的俱乐部会员资格。

            【讨论】:

              【解决方案18】:

              我使用的是 Windows 10,这对我有用:

              1. here 下载 geckodriver。为您使用的计算机下载正确的版本。
              2. 解压刚刚下载的文件并剪切/复制其中包含的“.exe”文件
              3. 导航到 C:{您的 python 根文件夹}。我的是 C:\Python27。将 geckodriver.exe 文件粘贴到此文件夹中。
              4. 重新启动您的开发环境。
              5. 再次尝试运行代码。它现在应该可以工作了。

              【讨论】:

                【解决方案19】:
                from webdriverdownloader import GeckoDriverDownloader # vs ChromeDriverDownloader vs OperaChromiumDriverDownloader
                gdd = GeckoDriverDownloader()
                gdd.download_and_install()
                #gdd.download_and_install("v0.19.0")
                

                这将为您提供 Windows 上 gekodriver.exe 的路径。

                from selenium import webdriver
                driver = webdriver.Firefox(executable_path=r'C:\\Users\\username\\\bin\\geckodriver.exe')
                driver.get('https://www.amazon.com/')
                

                【讨论】:

                  【解决方案20】:

                  如果您使用的是 Linux

                  ,则可以使用简单的命令解决此问题
                  1. 首先,下载 (https://github.com/mozilla/geckodriver/releases) 并解压 ZIP 文件

                  2. 打开解压后的文件夹

                  3. 从文件夹中打开终端(解压后geckodriver文件所在的位置)

                  4. 现在在终端上运行这个简单的命令,将 geckodriver 复制到正确的文件夹中:

                     sudo cp geckodriver /usr/local/bin
                    

                  【讨论】:

                    【解决方案21】:

                    Selenium 在他们的 DESCRIPTION.rst 文件中回答了这个问题:

                    驱动程序
                    =======

                    Selenium 需要驱动程序才能与所选浏览器交互。火狐, 例如,需要geckodriver &lt;https://github.com/mozilla/geckodriver/releases&gt;_,在运行以下示例之前需要安装它。确保它在您的PATH 中,例如。 g.,将其放在/usr/bin/usr/local/bin

                    不遵守此步骤将给您一个错误 `selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

                    基本上只需下载 geckodriver,解压缩并将可执行文件移动到您的 /usr/bin 文件夹。

                    【讨论】:

                    • 此引用包含在 linux mint 18 上工作的说明。
                    【解决方案22】:

                    对于 Windows 用户

                    按原样使用原始代码:

                    from selenium import webdriver
                    browser = webdriver.Firefox()
                    driver.get("https://www.google.com")
                    

                    然后从mozilla/geckodriver下载驱动

                    将其放置在固定路径中(永久)...例如,我将其放入:

                    C:\Python35

                    然后进入系统的环境变量。在“系统变量”的网格中查找 Path 变量并添加:

                    ;C:\Python35\geckodriver

                    geckodriver,不是 geckodriver.exe

                    【讨论】:

                    • geckodriver,不是 geckodriver.exe。
                    【解决方案23】:

                    如果您使用虚拟环境和 Windows 10(可能其他系统也一样),您只需将 geckodriver.exe 放入您的虚拟环境目录中的以下文件夹:

                    ...\my_virtual_env_directory\Scripts\geckodriver.exe

                    【讨论】:

                    • 正是...在环境的 lib(Unix) 或 Scripts(Windows) 目录中安装 geckodriver 有助于在使用虚拟环境时解决此问题。
                    【解决方案24】:

                    避免错误的一种新方法是使用 conda 环境。

                    使用conda install -c conda-forge geckodriver,您无需在路径中添加任何内容或编辑代码!

                    【讨论】:

                    • 我在 VSCode 中试过这个,能够使用 Selenium 使用 webdriver(用于 Firefox)运行 pytest。
                    【解决方案25】:

                    在 Raspberry Pi 上,我必须从 ARM 驱动程序创建并设置 geckodriver 和日志路径:

                    sudo nano /usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py
                    
                    def __init__(self, firefox_profile=None, firefox_binary=None,
                                 timeout=30, capabilities=None, proxy=None,
                                 executable_path="/PATH/gecko/geckodriver",                     
                    firefox_options=None,
                                 log_path="/PATH/geckodriver.log"):
                    

                    【讨论】:

                    • 创建什么?来自an ARM driver?来自the ARM driver(不止一个)?什么是“ARM 驱动程序”?你能详细说明一下吗?
                    【解决方案26】:

                    对我来说,只需在同一环境中安装 geckodriver 就足够了:

                    $ brew install geckodriver
                    

                    而且代码没有变:

                    from selenium import webdriver
                    browser = webdriver.Firefox()
                    

                    【讨论】:

                      【解决方案27】:

                      macOS v10.12.1 (Sierra) 和 Python 2.7.10 上,这对我有用:

                      def download(url):
                          firefox_capabilities = DesiredCapabilities.FIREFOX
                          firefox_capabilities['marionette'] = True
                          browser = webdriver.Firefox(capabilities=firefox_capabilities,
                                                      executable_path=r'/Users/Do01/Documents/crawler-env/geckodriver')
                          browser.get(url)
                          return browser.page_source
                      

                      【讨论】:

                        【解决方案28】:

                        访问 Gecko Driver 并从 下载 部分获取 Gecko 驱动程序的 URL。

                        克隆此存储库:https://github.com/jackton1/script_install.git

                        cd script_install

                        运行

                        ./installer --gecko-driver https://github.com/mozilla/geckodriver/releases/download/v0.18.0/geckodriver-v0.25.0-linux64.tar.gz
                        

                        【讨论】:

                          【解决方案29】:

                          我使用的是 Windows 10 和 Anaconda 2。我尝试设置系统路径变量,但没有成功。然后我简单地将 geckodriver.exe 文件添加到 Anaconda 2/Scripts 文件夹中,现在一切正常。

                          对我来说,路径是:

                          C:\Users\Bhavya\Anaconda2\Scripts
                          

                          【讨论】:

                            【解决方案30】:

                            如果您想在 Windows 10 上添加驱动程序路径:

                            1. 右键单击“这台电脑”图标并选择“属性”

                            2. 点击“高级系统设置”

                            3. 点击屏幕底部的“环境变量”

                            4. 在“用户变量”部分突出显示“路径”并单击“编辑”

                            5. 通过单击“新建”并键入要添加的驱动程序的路径并按 Enter 键,将路径添加到变量中。

                            6. 输入完路径后,点击“确定”

                            7. 继续点击“确定”,直到关闭所有屏幕

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 2021-04-15
                              • 2018-03-25
                              • 2017-05-17
                              • 2017-11-21
                              • 2018-07-11
                              相关资源
                              最近更新 更多