我对 Python 相当陌生,现在,我正在通过 Al Sweigart 的《用 Python 自动化无聊的事情》来学习 Python。
现在,我想使用 webdriver 打开 Firefox。当我运行代码时:
from selenium import webdriver
我没有收到任何错误。然而,运行后:
browswer=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#2>", line 1, in <module>
browswer=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.
现在,我已经安装了 geckodriver,并且我一直在谷歌上滚动以尝试找到解决方案。到目前为止,我已尝试手动将 geckodriver 添加到 /usr/local/bin 。我也尝试过跑步:
brew install geckodriver
在终端上。它建议我尝试覆盖geckodriver,因此brew将geckodriver安装在/usr/local/Cellar/geckodriver/0.21.0/bin/geckodriver上。
我已经跑了
sudo nano /etc/paths
查看我的路径列表,以下是列表:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/Cellar/geckodriver/0.21.0/bin/geckodriver
所以我不知道为什么 python 告诉我 geckodriver 可执行文件需要位于 PATH 中,而它已经存在了。
有人可以帮我吗?
我在 Mac OS 上遇到了与 OP 完全相同的问题。对我来说解决这个问题的方法是将 geckodriver 复制到 python 路径中。
具体来说,在终端中输入以下命令将显示 geckodriver 的路径:
which geckodriver
给出以下路径
/usr/local/bin/geckodriver
和
which python
给出Python的路径
/Users/USERNAME/anaconda3/bin/python
然后我将 geckodriver 复制到 python bin 中
cd /usr/local/bin/
cp geckodriver /Users/USERNAME/anaconda3/bin/
我能够从 Python 调用 geckodriver。