简体   繁体   English

selenium.common.exceptions.InvalidSessionIdException 使用 GeckoDriver Selenium Firefox 在无头模式下通过 Python

[英]selenium.common.exceptions.InvalidSessionIdException using GeckoDriver Selenium Firefox in headless mode through Python

I am tryin to use python an selenium to automate some cases on firefox headless mode.我正在尝试使用 python 和 selenium 在 firefox 无头模式下自动执行某些情况。 I think, I did every settings that I need but still webdrvier is not initialized.我想,我做了我需要的所有设置,但 webdrvier 仍然没有初始化。

My problem is getting the exception approximately 30-60 sec after executing my code and exception message is not clear actually message section is empty.我的问题是在执行我的代码后大约 30-60 秒出现异常,异常消息不清楚,实际上消息部分是空的。 As you can see on the geckodriver.log firefox running in headless mode.正如您在 geckodriver.log firefox 上看到的那样以无头模式运行。

I use Firefox 56.0.1, python 3.6, selenium 3.141.0.我使用 Firefox 56.0.1、python 3.6、selenium 3.141.0。 Here is my code and logs;这是我的代码和日志;

Code:代码:

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/")

Logs:日志:

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 logs:壁虎驱动程序日志:

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

Any ideas on what I am doing wrong?关于我做错了什么的任何想法? Thank you!谢谢!

This error message...这个错误信息...

selenium.common.exceptions.InvalidSessionIdException: Message:

...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowsing Session ie Firefox Browser session. ...暗示GeckoDriver无法启动/生成新的WebBrowsing 会话,Firefox 浏览器会话。

As per the log messages geckodriver version number is no longer reflected on startup, so you must be using:根据日志消息, geckodriver 版本号不再反映在启动时,因此您必须使用:

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

But you are using:但是您正在使用:

  • Selenium 3.141.0硒 3.141.0
  • Firefox 56.0.1火狐 56.0.1

So, your main issue is the incompatibility between the version of the GeckoDriver , Selenium and Firefox binaries you are using.因此,您的主要问题是您使用的GeckoDriverSeleniumFirefox二进制文件版本之间的不兼容


Solution解决方案

If you are using GeckoDriver - Selenium - Firefox combo, you need to follow the following compatibility chart for the Supported platforms :如果您使用GeckoDriver - Selenium - Firefox组合,您需要遵循以下支持平台的兼容性图表:

兼容性图表

This message also occured when switching virtual desktops in MS Windows. You asked:在 MS Windows 中切换虚拟桌面时也会出现此消息。您问:

Where do I find more informations about those combos?我在哪里可以找到有关这些组合的更多信息?

You could use a tool that automatically downloads the driver for you .您可以使用自动为您下载驱动程序的工具

from webdriver_manager.chrome import ChromeDriverManager
from seleniumrequests import Chrome

driver = Chrome(ChromeDriverManager().install())

暂无
暂无

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

相关问题 获取 selenium.common.exceptions.InvalidSessionIdException:消息:会话 ID 无效 - Getting selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id selenium.common.exceptions.InvalidSessionIdException:消息:无效的 session id - selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id selenium.common.exceptions.NoSuchElementException:消息:在将 GeckoDriver Firefox 和 Selenium 与 Python 一起使用之前未看到 Web 元素引用 - selenium.common.exceptions.NoSuchElementException: Message: Web element reference not seen before using GeckoDriver Firefox and Selenium with Python Selenium Python selenium.common.exceptions.WebDriverException:消息:使用geckodriver和firefox拒绝连接 - Selenium Python selenium.common.exceptions.WebDriverException: Message: connection refused using geckodriver and firefox selenium.common.exceptions.WebDriverException:消息:使用 GeckoDriver Firefox Selenium Python 时,“Geckodriver”可执行文件可能具有错误的权限 - selenium.common.exceptions.WebDriverException: Message: 'Geckodriver' executable may have wrong permissions using GeckoDriver Firefox Selenium Python 504 网关超时错误使用 Firefox 68.9.0esr 在无头模式下使用 GeckoDriver Selenium 和 Python - 504 Gateway Time-out error using Firefox 68.9.0esr in headless mode with GeckoDriver Selenium and Python selenium.common.exceptions.WebDriverException:消息:&#39;firefox&#39; 可执行文件需要在 GeckoDriver Firefox Selenium 和 Python 的 PATH 中 - selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python selenium.common.exceptions.WebDriverException:消息:“ geckodriver”可执行文件必须与GeckoDriver Selenium Firefox一起放入PATH - selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH with GeckoDriver Selenium Firefox Python Selenium 和 FireFox \\ geckodriver - Python Selenium and FireFox \ geckodriver AttributeError: 'Options' object has no attribute 'binary' error invoking Headless Firefox using GeckoDriver through Selenium - AttributeError: 'Options' object has no attribute 'binary' error invoking Headless Firefox using GeckoDriver through Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM