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 geckodriver
,which geckodriver
返回/usr/local/bin/geckodriver
,所以我确定它已正确安装。好像它似乎还没有正常运行?
答案 0 :(得分:3)
您的错误消息非常明确:
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 os中的/usr/local/bin/
中移动可执行文件。我试过一个ubuntu操作系统,它的工作原理。我认为应该没问题。
可能是因为该文件不可执行。如果不是,请转到/usr/local/bin/
并将其设为可执行文件:
chmod +x geckodriver
答案 1 :(得分:3)
我正在使用ubuntu 18.04,在这里遇到了同样的问题。
就我而言,这很好。希望对您有用!
sudo apt-get install firefox-geckodriver
答案 2 :(得分:0)
最适合我的事情之一就是复制可执行的geckodriver文件。
进入bin文件夹:
如果您使用的是Linux,则可以在以下位置找到它:
/home/user_name/.local/bin
在此处粘贴您的geckodriver exe。
因此您的最终代码应如下所示:
from selenium import webdriver
driver = webdriver.Firefox(executable_path = 'geckodriver')
这对我来说就像魅力一样起作用,但是不确定是否正确。