'geckodriver' 可执行文件需要在 PATH 中

新手上路,请多包涵

Mac OS 用户在这里。我正在尝试在我的 python IDLE 中运行一个命令:

 from selenium import webdriver
browser = webdriver.Firefox()

我收到以下错误消息:

 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.

我已经运行了 brew install geckodriverwhich geckodriver 返回 /usr/local/bin/geckodriver 所以我确定它安装正确。似乎它仍然无法正常运行?

原文由 doctopus 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 582
1 个回答

你的错误信息很清楚:

 FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'

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

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

 from selenium import webdriver
browser = webdriver.Firefox(executable_path=r'/.../path2Your/geckodriver')

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

 export PATH=$PATH:/.../path2Your/geckodriver

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

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

 chmod +x geckodriver

原文由 Davide Patti 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题