问题描述
Mac OS用户在此处.我正在尝试在python IDLE中运行命令:
Mac OS user here. I'm trying to run a command in my python IDLE:
from selenium import webdriver
browser = webdriver.Firefox()
,我收到以下错误消息:
and I get the following error message:
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 geckodriver
,并且which geckodriver
返回/usr/local/bin/geckodriver
,所以我确定它已正确安装.看来它仍然无法正常运行?
I've ran brew install geckodriver
, and which geckodriver
returns /usr/local/bin/geckodriver
so I'm sure it's installed correctly. It seems like it's still not running properly however?
推荐答案
您的错误消息非常清楚:
Your error message is quite clear:
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'
您必须将路径设置为壁虎驱动程序.有不同的方法可以做到这一点.
You have to set the path to the geckodriver. There are different ways to do that.
您可以在代码中设置壁虎驱动程序的路径:
You could set the path to the geckodriver in the code:
from selenium import webdriver
browser = webdriver.Firefox(executable_path=r'/.../path2Your/geckodriver')
或在 PATH 环境变量中插入壁虎驱动程序的路径:
or insert the path to the geckodriver in the PATH environment variable:
export PATH=$PATH:/.../path2Your/geckodriver
我从未尝试过在Mac OS的/usr/local/bin/
中移动可执行文件.我在ubuntu操作系统中尝试过,它可以工作.我认为应该没问题.
I never tried to move the executable in the /usr/local/bin/
in a mac os. I tried in an ubuntu os and it works. I think should be fine.
可能是因为该文件不可执行.如果不是,请转到/usr/local/bin/
并将其设置为可执行文件:
Probably it's because the file is not executable. If it isn't, go to /usr/local/bin/
and make it executable:
chmod +x geckodriver
这篇关于'geckodriver'可执行文件必须在PATH中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!