【发布时间】:2022-01-24 17:38:27
【问题描述】:
我正在尝试在 Google Colab 中使用 Selenium,但在尝试运行 Firefox 实例时出现了一些错误。我关注了这个链接:
- Selenium Documentation 在这里,我尝试使用驱动程序管理软件,但出现错误,提示无法找到 Firefox 的二进制位置。所以我尝试了Hard Coded Location方法,但还是出现同样的错误,所以我认为我必须在google colab中安装firefox,但我不知道这是否是正确的方法
这是我的代码:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
service = FirefoxService(executable_path= '/content/drive/MyDrive/geckodriver')
driver = webdriver.Firefox(service=service)
这是整个错误:
SessionNotCreatedException Traceback (most recent call last)
<ipython-input-7-51ea1584f592> in <module>()
1 service = FirefoxService(executable_path= '/content/drive/MyDrive/geckodriver')
----> 2 driver = webdriver.Firefox(service=service)
4 frames
/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
245 alert_text = value['alert'].get('text')
246 raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
--> 247 raise exception_class(message, screen, stacktrace)
248
249 def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:
SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
编辑
我尝试在 Google colab 上安装 firefox,我认为一切都是正确的,但是当它尝试运行 firefox 时,我在 geckodriver.log 上得到了这个输出:
1643070515477 geckodriver INFO Listening on 127.0.0.1:43355
1643070515979 mozrunner::runner INFO Running command: "/usr/bin/firefox" "--marionette" "--headless" "--no-sandbox" "--disable-dev-shm-usage" "--remote-debugging-port" "43945" "-no-remote" "-profile" "/tmp/rust_mozprofileeDGKcf"
src/tcmalloc.cc:283] Attempt to free invalid pointer 0x7fe67e416250
Redirecting call to abort() to mozalloc_abort
代码如下:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
service = FirefoxService(executable_path= '/content/drive/MyDrive/geckodriver')
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Firefox(service= service, options= options)
【问题讨论】:
-
这不是在这里stackoverflow.com/questions/70836496/…@ 解决的吗?
-
@cruisepandey,那是关于我得到的另一个错误,但这是另一个错误,所以我认为最好是另一个问题。
-
听起来你需要设置 Firefox 的路径。 (浏览器,而不是驱动程序)也许看到这个帖子:stackoverflow.com/questions/58296262/…
-
@pcalkins 是的,我需要,但问题是我是否必须在 google colab 中安装 firefox,或者有更好的解决方案。
-
您需要将它安装在运行 webdriver 的机器上,或者安装在运行远程端/网格的机器上。
标签: python selenium selenium-webdriver google-colaboratory geckodriver