selenium.common.exceptions.InvalidSessionIdException using GeckoDriver Selenium Firefox in headless mode through Python
Asked Answered
W

2

5

I am tryin to use python an selenium to automate some cases on firefox headless mode. I think, I did every settings that I need but still webdrvier is not initialized.

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. As you can see on the geckodriver.log firefox running in headless mode.

I use 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!

Weathersby answered 12/12, 2018 at 9:27 Comment(1)
This is not the same question. On that question the owner of question set the firefox as a headless but still firefox open as regular browser with head but in my case there is no regular firefox browser with head. If you can look at the geckodriver.log that I shared, you will see firefox is running in headless mode. My problem is getting the exception approximately 30-60 sec after executing my code.Weathersby
B
11

This error message...

selenium.common.exceptions.InvalidSessionIdException: Message:

...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowsing Session i.e. Firefox Browser session.

As per the log messages geckodriver version number is no longer reflected on startup, so you must be using:

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

But you are using:

  • Selenium 3.141.0
  • Firefox 56.0.1

So, your main issue is the incompatibility between the version of the GeckoDriver, Selenium and Firefox binaries you are using.


Solution

If you are using GeckoDriver-Selenium-Firefox combo, you need to follow the following compatibility chart for the Supported platforms:

compatibility chart

Burnejones answered 12/12, 2018 at 14:54 Comment(2)
it worked. Thank you so much but I do not understand one thing. I spent a lot of time to find that solution but I couldn't. Where do I find more informations about those combos?@DebanjanBWeathersby
@KaanTamersoy I don't have a canonical answer for this counter question. However the error stack trace will always push you towards the right direction as it happened in your case. But for a better understanding you have to go through each and every related documentation you come across. Once you start answering on StackOverflow you can learn from the mistakes at the same time gain confidence from the positive vibes.Burnejones
A
2

This message also occured when switching virtual desktops in MS Windows. You asked:

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())
Alten answered 25/2, 2022 at 16:5 Comment(1)
ironically i already used thisCoif

© 2022 - 2024 — McMap. All rights reserved.