'Selenium4 and Snap install of Firefox with Geckodriver_v31 times out

I recently upgraded lubuntu 22.04 and it wanted a few things to be installed from the snap repository. Firefox was one of them. Currently I'm using Selenium 4.1.3, Python 3.10 and Firefox 99.0.1 with latest geckodriver V31.0

I've been using this python3 code for my testing for some time but now it completely fails to start.

First of it failed to find a profile, so I forced something in there:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

options = Options()
options.add_argument("-profile /path2temp/")  # create profile
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting",
                       False)
options.set_preference("browser.download.dir", "./data_export")
options.set_preference(
    "browser.helperApps.neverAsk.saveToDisk",
    "application/vnd.google-earth.kml+xml,application/x-zip-compressed,application/gpx+xml,text/csv"
)
options.set_preference("devtools.debugger.remote-enabled", True)
options.set_preference("devtools.debugger.prompt-connection", False)

browser = webdriver.Firefox(options=options, executable_path=r"/usr/bin/geckodriver")

url = 'https://cnn.com'
browser.get(url)

If firefox is already open, it fails to communicate with it. Normally in the past it would just open a new tab and start working. But now I get this error:

Firefox is already running, but is not responding. To use Firefox, you must first close the existing Firefox process, restart your device, or use a different profile.

If I let it initiate the application, it then times out after a lot of time with the following error (note, the /path2temp/ is a real path to a directory where it has permissions).

1651528082918   geckodriver     
INFO    Listening on 127.0.0.1:54985 1651528083062   mozrunner::runner       
INFO    Running command: "/snap/bin/firefox" "--marionette" "-profile /path2temp/" "--remote-debugging-port" "47927" "-- remote-allow-hosts" "localhost" "-no-remote" 
ATTENTION: default value of option mesa_glthread overridden by environment. 
ATTENTION: default value of option mesa_glthread overridden by environment. 
ATTENTION: default value of option mesa_glthread overridden by environment. 
ATTENTION: default value of option mesa_glthread overridden by environment. 
DevTools listening on ws://localhost:47927/devtools/browser/19a59834-6a4b-4d75-902c-06c36704d50e 
Exiting due to channel error. 
Exiting due to channel error. 
Exiting due to channel error. 
Exiting due to channel error. 
Exiting due to channel error.

Any ideas of what I could do to fix this problem?


Edit: I was able to at least get it to work when it initiates firefox by passing it to the current users profile located in the snap file structure /home/username/snap/firefox/common/.mozilla/firefox/wnrrbapq.default-release

But it's not an ideal behavior as I have to close the browser every time for testing.



Solution 1:[1]

Snap version of Firefox cannot write to /tmp location. Recommended workaround is to set TMPDIR environment variable to location that both geckodriver and firefox can write to e.g. $HOME/tmp.

More info in geckdriver 0.31.0 relaese notes and related github issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Łukasz Korbel