簡體   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