Python ';壁虎河';可执行文件需要在路径中

Python ';壁虎河';可执行文件需要在路径中,python,path,geckodriver,Python,Path,Geckodriver,这里是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

这里是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.
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/selenium/webdriver/common/service.py”,第76行,开头
标准偏差=管道)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py”,第709行,在__
恢复信号,启动新会话)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py”,第1344行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“geckodriver”:“geckodriver”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
browser=webdriver.Firefox()
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/selenium/webdriver/firefox/webdriver.py”,第160行,在__
self.service.start()
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py”,第83行,开头
os.path.basename(self.path)、self.start\u错误消息)
selenium.common.exceptions.WebDriverException:消息:“geckodriver”可执行文件需要位于路径中。

我已经运行了
brew安装geckodriver
,以及
geckodriver
返回的
/usr/local/bin/geckodriver
,因此我确信它安装正确。但是,它似乎仍无法正常运行?

您的错误消息非常清楚:

FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
您必须设置通往壁虎河的路径。有不同的方法可以做到这一点

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

from selenium import webdriver
browser = webdriver.Firefox(executable_path=r'/.../path2Your/geckodriver')
或者将geckodriver的路径插入环境变量中:

export PATH=$PATH:/.../path2Your/geckodriver
我从未尝试在mac os中移动
/usr/local/bin/
中的可执行文件。我在ubuntu操作系统中试过了,效果很好。我想应该没问题

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

chmod +x geckodriver

对我来说最有效的方法之一就是复制可执行的geckodriver文件

进入bin文件夹:

如果您使用的是linux,您可以在以下位置找到它:

/home/user_name/.local/bin
将geckodriver exe粘贴到此处

因此,您的最终代码应该如下所示:

    from selenium import webdriver
    driver = webdriver.Firefox(executable_path = 'geckodriver')

这对我来说很有吸引力,但我不确定这是否正确。

我使用的是ubuntu 18.04,这里也有同样的问题

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

sudo apt-get install firefox-geckodriver

非常感谢。在我的例子中,可执行路径选项运行良好