【问题标题】:WebDriverException: 'geckodriver' executable needs to be in PATH even though it isWebDriverException:'geckodriver' 可执行文件需要在 PATH 中,即使它是
【发布时间】:2018-10-01 12:40:47
【问题描述】:

尝试在 spyder 中使用 selenium 让 firefox 运行。我当前的代码是

from selenium import webdriver
import os
os.environ["PATH"] += ":/usr/local/bin/geckodriver"
browser = webdriver.Firefox()

我仍然收到此错误:

runfile('/Users/mherl/Dropbox/AnacondaProjects/MWS/MWSSpyder/test.py', 
wdir='/Users/mherl/Dropbox/AnacondaProjects/MWS/MWSSpyder')
Traceback (most recent call last):

  File "<ipython-input-1-3f3f96ccf515>", line 1, in <module>

runfile('/Users/mherl/Dropbox/AnacondaProjects/MWS/MWSSpyder/test.py', 
wdir='/Users/mherl/Dropbox/AnacondaProjects/MWS/MWSSpyder')

  File "/Applications/anaconda3/lib/python3.6/site- 
  packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
  execfile(filename, namespace)

  File "/Applications/anaconda3/lib/python3.6/site- 
  packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/mherl/Dropbox/AnacondaProjects/MWS/MWSSpyder/test.py", 
line 12, in <module>
  browser = webdriver.Firefox()

  File "/Applications/anaconda3/lib/python3.6/site- 
packages/selenium/webdriver/firefox/webdriver.py", line 152, in 
__init__
self.service.start()

  File "/Applications/anaconda3/lib/python3.6/site- 
packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)

WebDriverException: 'geckodriver' executable needs to be in PATH. 

即使 gekodriver 在该文件夹中。

我也尝试将路径导出到 ~./bash_profile,现在看起来像这样。

Last login: Fri Apr 20 10:57:16 on ttys000
dhcp-54-85:~ mherl$ nano ./bash_profile
dhcp-54-85:~ mherl$ nano .bash_profile
  GNU nano 2.0.6             File: .bash_profile                      
Modified  


# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

# added by Anaconda3 5.1.0 installer
export PATH="/Applications/anaconda3/bin:$PATH"

#added by mherl to show path to gekodriver
export PATH=$PATH:/usr/local/bin/gekodriver

我还在 spyder 中设置了当前路径:

/usr/local/bin
/Users/mherl/Dropbox/AnacondaProjects/MWS/MWSSpyder

其中gekodriver在

/usr/local/bin

我到处找过,大多数人说它应该在 /usr/local/bin 中自动运行,但即使我明确指出路径,它似乎仍然找不到它。

这是一个运行 High Sierra 的 mac,如果这很重要的话。

【问题讨论】:

    标签: macos selenium firefox selenium-webdriver geckodriver


    【解决方案1】:

    此错误消息...

    WebDriverException: 'geckodriver' executable needs to be in PATH. 
    

    ...暗示 GeckoDriver 未在预期的默认位置找到。

    解决方案

    当您使用 MAC based System 时,您需要传递 Key executable_path 以及 Value 引用到GeckoDriver绝对路径如下:

    from selenium import webdriver
    
    browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
    

    其他注意事项

    确保以下几点:

    • GeckoDriver 存在于指定位置。
    • GeckoDriver 对非 root 用户具有可执行权限。
    • 以非 root 用户身份执行 @Test。

    【讨论】:

    • 这确实有效,但 macOS 是什么要求你这样做?
    • @MakennaSophiaHerl 不仅是 macOS,而且每个操作系统的行为几乎都相似。
    • 在 ubuntu(&pycharm) 上添加 executable_path 参数也对我有用!非常感谢@DebanjanB,当我看到我的 PATH 环境变量的值是正确的时,我疯了为什么它没有运行! :)
    • 我正在运行 linux manjaro。 geckodriver 是我默认应该拥有的东西还是我需要下载它?关于我可以在我的系统上哪里找到它的任何提示?我找到了一个 geckodriver.log 文件(打开时它是空的)
    猜你喜欢
    • 2017-03-04
    • 2018-10-31
    • 2020-06-05
    • 1970-01-01
    • 2017-11-21
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多