'geckodriver'可执行文件必须在PATH中

doc*_*pus 4 python path geckodriver

Mac OS用户在此处。我正在尝试在python IDLE中运行命令:

from selenium import webdriver
browser = webdriver.Firefox()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    browser = webdriver.Firefox()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 160, in __init__
    self.service.start()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Run Code Online (Sandbox Code Playgroud)

我已经跑了brew install geckodriver,然后which geckodriver返回,/usr/local/bin/geckodriver所以我确定它安装正确。看来它仍然无法正常运行?

小智 6

我使用的是 ubuntu 18.04,这里也遇到了同样的问题。

就我而言,这效果很好。希望这对你有用!

sudo apt-get install firefox-geckodriver
Run Code Online (Sandbox Code Playgroud)


Dav*_*tti 5

您的错误信息非常清楚:

FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
Run Code Online (Sandbox Code Playgroud)

您必须设置geckodriver的路径。有不同的方法可以做到这一点。

您可以在代码中设置geckodriver的路径:

from selenium import webdriver
browser = webdriver.Firefox(executable_path=r'/.../path2Your/geckodriver')
Run Code Online (Sandbox Code Playgroud)

或在PATH环境变量中插入geckodriver的路径

export PATH=$PATH:/.../path2Your/geckodriver
Run Code Online (Sandbox Code Playgroud)

我从未尝试过/usr/local/bin/在Mac OS中移动可执行文件。我在ubuntu操作系统中尝试过,它可以工作。我认为应该没问题。

可能是因为该文件不可执行。如果不是,请转到/usr/local/bin/可执行文件:

chmod +x geckodriver
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你。在我的情况下,可执行路径选项工作正常 (2认同)