我正在学习“用 Python 自动化无聊的事情”,并在运行 OS X v. 10.9.5 的 Mac 上运行 Python 3.6。我的 Firefox 版本是 50.0。
每当我输入时(按照第 11 章中的说明):
>>> 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 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1326, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#18>", 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 140, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
我已在此网站和其他网站上搜索过此错误,但提供的大多数解决方案都是特定于 Windows 的(包括下面评论中引用的帖子),或者应用后似乎无法在我的 Mac 计算机上运行。
关于我应该做什么有什么建议吗?
请记住,我完全是新手,因此对终端不太熟悉。预先感谢您的帮助。
最佳答案
我深入研究了 webdriver 和 Firefox 驱动程序的源代码:我必须为 Firefox() 构造函数提供指向 geckodriver 二进制文件的直接路径,例如
self.browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
关于python - 在 Mac 上使用 Python 和 Firefox 的 selenium webdriver 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41385530/