【问题标题】:selenium.common.exceptions.InvalidSessionIdException using GeckoDriver Selenium Firefox in headless mode through Pythonselenium.common.exceptions.InvalidSessionIdException 通过 Python 在无头模式下使用 GeckoDriver Selenium Firefox
【发布时间】:2019-05-13 08:54:13
【问题描述】:

我正在尝试使用 python 和 selenium 在 Firefox 无头模式下自动执行某些情况。我想,我做了所有我需要的设置,但仍然没有初始化 webdrvier。

我的问题是在执行我的代码后大约 30-60 秒出现异常,并且异常消息不清楚实际上消息部分是空的。正如您在以无头模式运行的 geckodriver.log firefox 所见。

我使用 Firefox 56.0.1、python 3.6、selenium 3.141.0。这是我的代码和日志;

代码:

import os
import time
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

options = Options()
options.set_headless(True)
dir = "C:\\Python36\\Lib\\site-packages\\selenium\\webdriver\\firefox"
ff_driver_path = dir + "\\geckodriver.exe"
firefox_capabilities = DesiredCapabilities().FIREFOX
firefox_capabilities['marionette'] = True
profile = webdriver.FirefoxProfile()
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(desired_capabilities=firefox_capabilities, firefox_profile=profile, firefox_binary=binary, firefox_options=options, executable_path=ff_driver_path)
driver.get("https://ipaddress/")

日志:

File "<string>", line 20, in <module>
File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 174, in __init__
    keep_alive=True)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message:

geckodriver 日志:

1544600615819   mozrunner::runner   INFO    Running command: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\2\\rust_mozprofile.SDw8B2IEG5cE"
*** You are running in headless mode.
1544600616120   addons.xpi  WARN    Error parsing extensions state: [Exception... "Component returned failure code: 0x80520012 (NS_ERROR_FILE_NOT_FOUND) [amIAddonManagerStartup.readStartupData]"  nsresult: "0x80520012 (NS_ERROR_FILE_NOT_FOUND)"  location: "JS frame :: resource://gre/modules/addons/XPIProvider.jsm :: loadExtensionState :: line 1554"  data: no] Stack trace: loadExtensionState()@resource://gre/modules/addons/XPIProvider.jsm:1554 < getInstallState()@resource://gre/modules/addons/XPIProvider.jsm:1589 < checkForChanges()@resource://gre/modules/addons/XPIProvider.jsm:3109 < startup()@resource://gre/modules/addons/XPIProvider.jsm:2188 < callProvider()@resource://gre/modules/AddonManager.jsm:269 < _startProvider()@resource://gre/modules/AddonManager.jsm:739 < startup()@resource://gre/modules/AddonManager.jsm:906 < startup()@resource://gre/modules/AddonManager.jsm:3090 < observe()@jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/addonManager.js:65
1544600616661   Marionette  INFO    Enabled via --marionette
[Child 2656] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 2656] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
1544600618075   Marionette  INFO    Listening on port 64093
JavaScript error: jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/captivedetect.js, line 352: TypeError: this._runningRequest is null
JavaScript error: jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/captivedetect.js, line 352: TypeError: this._runningRequest is null
JavaScript error: jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/captivedetect.js, line 352: TypeError: this._runningRequest is null
JavaScript error: jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/captivedetect.js, line 352: TypeError: this._runningRequest is null
JavaScript error: jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/captivedetect.js, line 352: TypeError: this._runningRequest is null
JavaScript error: jar:file:///C:/Program%20Files%20(x86)/Mozilla%20Firefox/omni.ja!/components/captivedetect.js, line 352: TypeError: this._runningRequest is null

关于我做错了什么有什么想法吗?谢谢!

【问题讨论】:

  • 这不是同一个问题。在那个问题上,问题的所有者将 firefox 设置为无头但仍将 firefox 作为带 head 的常规浏览器打开,但在我的情况下,没有带 head 的常规 firefox 浏览器。如果您可以查看我分享的 geckodriver.log,您会看到 Firefox 正在以无头模式运行。我的问题是在执行我的代码后大约 30-60 秒出现异常。
  • 您的评论清楚地表明它不是重复的,并且您的评论从未到达我的收件箱。继续前进,当您回复特定用户时,请在 usename 之前添加@,以便将评论发送到特定用户的收件箱并提示。 重新开始讨论。很抱歉给您带来不便。

标签: python selenium geckodriver selenium-firefoxdriver firefox-headless


【解决方案1】:

此错误消息...

selenium.common.exceptions.InvalidSessionIdException: Message:

...暗示 GeckoDriver 无法启动/产生新的 WebBrowsing 会话,即 Firefox 浏览器 会话。

根据日志消息 geckodriver 版本号 不再反映在启动时,因此您必须使用:

GeckoDriver 版本 => 0.22.0 (2018-09-15)

但你正在使用:

  • 硒 3.141.0
  • 火狐 56.0.1

所以,您的主要问题是 GeckoDriver、Selenium 和 Firefox 二进制文件版本之间的不兼容你正在使用。


解决方案

如果您使用的是 GeckoDriver-Selenium-Firefox 组合,则需要遵循以下Supported platforms 的兼容性图表:

【讨论】:

  • 成功了。非常感谢,但我不明白一件事。我花了很多时间来找到那个解决方案,但我做不到。我在哪里可以找到有关这些组合的更多信息?@DebanjanB
  • @KaanTamersoy 对于这个反问题我没有规范的答案。但是,错误堆栈跟踪将始终将您推向正确的方向,就像您的情况一样。但是为了更好地理解,您必须仔细阅读您遇到的每一个相关文档。一旦您开始在 StackOverflow 上回答问题,您就可以从错误中吸取教训,同时从积极的氛围中获得信心。
猜你喜欢
  • 2020-10-24
  • 2017-05-24
  • 2020-03-11
  • 2018-07-04
  • 2019-06-28
  • 1970-01-01
相关资源
最近更新 更多