Categories
python

Ubuntu 22.04 + Firefox + Selenium + Geckodriver: Connection refused

The Ubuntu 22.04 + Firefox with Selenium and Geckodriver problem is:

selenium.common.exceptions.TimeoutException: Message: Connection refused (os error 111)

An older firefox version was installed from source after looking at geckodriver’s supported platforms docs

Environment details:

$ firefox --version
Mozilla Firefox 89.0.2

$ geckodriver --version
geckodriver 0.29.1 (970ef713fe58 2021-04-08 23:34 +0200)

selenium                  3.14.1

Example script:

from selenium import webdriver
from selenium.webdriver import FirefoxOptions

opts = FirefoxOptions()
opts.add_argument('--headless')

browser = webdriver.Firefox(options=opts)

response = browser.get('https://number1.co.za')

browser.quit()

Running the above script with xvfb:

xvfb-run python3 script.py

It will hang and eventually error out with:

selenium.common.exceptions.TimeoutException: Message: Connection refused (os error 111)

The full trace log geckodriver.log:

1667754289214   geckodriver     INFO    Listening on 127.0.0.1:57393
1667754289222   webdriver::server       DEBUG   -> POST /session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts": true, "moz:firefoxOptions": {"binary": "/usr/bin/firefox", "args": ["-headless"], "log": {"level": "trace"}}}}, "desiredCapabilities": {"browserName": "firefox", "acceptInsecureCerts": true, "marionette": true, "moz:firefoxOptions": {"binary": "/usr/bin/firefox", "args": ["-headless"], "log": {"level": "trace"}}}}
1667754289223   geckodriver::capabilities       DEBUG   Trying to read firefox version from ini files
1667754289224   geckodriver::capabilities       DEBUG   Found version 89.0.2
1667754289228   mozrunner::runner       INFO    Running command: "/usr/bin/firefox" "--marionette" "-headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileE9bFOZ"
1667754289233   geckodriver::marionette DEBUG   Waiting 60s to connect to browser on 127.0.0.1:40777
*** You are running in headless mode.
[GFX1-]: glxtest: libEGL missing
1667754289986   Marionette      INFO    Marionette enabled
1667754290059   Marionette      TRACE   Received observer notification toplevel-window-ready

###!!! [Parent][MessageChannel] Error: (msgtype=0x230098,name=PBrowser::Msg_LoadRemoteScript) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x390143,name=PContent::Msg_CommitBrowsingContextTransaction) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x230072,name=PBrowser::Msg_UpdateDimensions) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x390143,name=PContent::Msg_CommitBrowsingContextTransaction) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x230057,name=PBrowser::Msg_UpdateEffects) Channel error: cannot send/recv

[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt
[GFX1-]: RenderCompositorSWGL failed mapping default framebuffer, no dt
console.warn: SearchSettings: "get: No settings file exists, new profile?" (new NotFoundError("Could not open the file at /tmp/rust_mozprofileE9bFOZ/search.json.mozlz4", (void 0)))

###!!! [Parent][MessageChannel] Error: (msgtype=0x390029,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x390029,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x390029,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv

###!!! [Parent][MessageChannel] Error: (msgtype=0x390029,name=PContent::Msg_PreferenceUpdate) Channel error: cannot send/recv

A content process crashed and MOZ_CRASHREPORTER_SHUTDOWN is set, shutting down
A content process crashed and MOZ_CRASHREPORTER_SHUTDOWN is set, shutting down
console.warn: SearchService: "_init: abandoning init due to shutting down"
JavaScript error: , line 0: uncaught exception: 2147500036
JavaScript error: , line 0: uncaught exception: 2147500036
JavaScript error: , line 0: uncaught exception: 2147500036
JavaScript error: , line 0: uncaught exception: 2147500036
A content process crashed and MOZ_CRASHREPORTER_SHUTDOWN is set, shutting down
JavaScript error: resource://gre/modules/CrashManager.jsm, line 829: AbortError: IOUtils: Shutting down and refusing additional I/O tasks
1667754349311   mozrunner::runner       DEBUG   Killing process 169241
1667754349356   webdriver::server       DEBUG   <- 500 Internal Server Error {"value":{"error":"timeout","message":"Connection refused (os error 111)","stacktrace":""}}

You could also use this test script:

if __name__ == "__main__":
    options = webdriver.firefox.options.Options()
    options.log.level = "trace"
    options.binary_location = "/usr/bin/firefox"
    options.add_argument("-headless")
    driver = webdriver.Firefox(options=options, service_args=["-v"])
    print(driver.capabilities["browserVersion"])
    driver.quit()

The Issue

The issue seemingly related to Github geckodriver issue: geckodriver does not work with snap packaged firefox?

How to Fix?

Use the apt firefox version for ubuntu 22.04

The steps in that post summarised:

sudo snap remove firefox
sudo add-apt-repository ppa:mozillateam/ppa
sudo apt install firefox

After installation, the firefox version in this case:

$ firefox --version
Mozilla Firefox 106.0.5

Shout out to Simple Snip Code on Youtube for this video: "Selenium error Your Firefox profile cannot be loaded in ubuntu 22.04"

Snap Firefox Rides Again

The problem is that ubuntu will default to use snap the next time an apt upgrade is run. Or perhaps it was unattended updates.

Recommendation is to kill snap off the machine using these instructions.

Some commands:

 1680  sudo snap remove firefox
 1681  sudo systemctl disable snapd.service
 1682  sudo systemctl disable snapd.socket
 1683  sudo systemctl disable snapd.seeded.service
 1684  sudo snap list
 1685  sudo snap remove snap-store
 1686  sudo snap remove lxd
 1687  sudo snap remove gtk-common-themes
 1688  sudo snap remove gnome-3-38-2004
 1689  sudo snap remove core20
 1690  sudo snap remove bare 
 1691  sudo snap list
 1692  sudo snap remove snapd
 1693  sudo apt autoremove --purge snapd
 1694  sudo rm -rf /var/cache/snapd/
 1695  rm -rf ~/snap