繁体   English   中英

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

[英]Problems with selenium webdriver using Python and Firefox on a Mac

我正在研究“用 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')

谢谢大家的回复。

根据评论,安装 Homebrew 并运行

brew install geckodriver

命令并不能单独解决问题。 重新启动终端也没有解决问题。

我翻阅了其他一些帖子,并尝试通过 Mac Finder 打开 /usr/bin 和 /usr/local/bin 文件,并手动将 geckodriver 文件复制到文件夹中。 我第一次进入:

browser = webdriver.Firefox()

进入 Python IDLE,我收到以下警告/异常:

Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x101a960b8>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 173, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 145, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x1071b3eb8>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 173, in __del__
    self.stop()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 145, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'

但是浏览器打开了。 当我再次运行它时,我能够按照书中的说明进行操作,没有任何错误或异常。

这似乎奏效了。 然而,通过 Mac finder 而不是终端来完成这一切感觉是错误的。 我对这一切仍然很陌生,所以如果我做的任何事情会损坏我的系统,我将不胜感激。 谢谢你的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM