python - 在 Mac 上使用 Python 和 Firefox 的 selenium webdriver 问题

标签 python macos selenium firefox

我正在学习“用 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/

相关文章:

python - 如何确保 re.findall() 停在正确的位置?

macos - 如何使用 nsdocument 架构在一个窗口中编辑多个文档

php - PHP_EOL在Mac上不起作用

Swift AVAudioPlayer (Xcode) - 在应用程序包外部播放音频文件

javascript - WebdriverIO 遍历元素列表

java - 在 Maven Selenium 项目的 TFS 中找不到 chromedriver 可执行文件

python - 使用 selenium ide 和 python 在 xpath 进行两次循环迭代后,我的程序失败

python - 寻找最小空间距离

python:如何获取有关函数的信息?

python嵌套json到带有指定标题的csv/xlsx