6

FIXED: While upgrading Ubuntu 21.04 to 21.10 firefox (previously installed with apt) got removed and installed with the snap-version. Reversing (uninstalling the snap version & reinstalling with apt) this fixed my issue.

I should've investigated after I had to reset firefox to be the default browser after the dist-upgrade again.


When trying to create a Firefox Webdriver with Selenium for Python I get greeted with the following: "Your Firefox profile cannot be loaded It may be missing or inaccessible." And after clicking 'ok' the following stack trace appears:

>>> foo = webdriver.Firefox()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1

This has not happened before upgrading to Ubuntu 21.10 (from 21.04) but I do not know what could cause this error. Manually trying to create the driver with the old Geckodriver (0.29.1) does not work too and results in the same error:

$ ./geckodriver.bup -V
geckodriver 0.29.1 (970ef713fe58 2021-04-08 23:34 +0200)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
$ python3
Python 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> foo = webdriver.Firefox(executable_path=r'./geckodriver.bup')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/jonathan/.local/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1

The only thing I am positive that changed was the python version (to 3.9.7 from 3.9.5, but using a python-venv with 3.9.5 produces the same errors).

Is there a glaring error I am missing?

Software-Versions/logs:

$ cat geckodriver.log 
1635171867200   geckodriver INFO    Listening on 127.0.0.1:37079
1635171867204   mozrunner::runner   INFO    Running command: "/snap/bin/firefox" "--marionette" "-no-remote" "-profile" "/tmp/rust_mozprofileZ5vGbH"
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
amdgpu: os_same_file_description couldn't determine if two DRM fds reference the same file description.
If they do, bad things may happen!
$ geckodriver -V
geckodriver 0.30.0 (d372710b98a6 2021-09-16 10:29 +0300)

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
$ firefox -V
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
Mozilla Firefox 93.0
$ pip freeze | grep selenium
selenium==3.141.0

(e: trying with selenium==4.0.0 also did not work)

$ python3 -V
Python 3.9.7
$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 21.10"
NAME="Ubuntu"
VERSION_ID="21.10"
VERSION="21.10 (Impish Indri)"
VERSION_CODENAME=impish
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=impish

e2: I suspect the error to be with permissions of the temporary profiles that get created. But the profiles created in '/tmp/' look clean. They are always set to the current user. Specifying them manually with e.g.

foo = webdriver.Firefox(firefox_profile=r'/tmp/rust_mozprofile7nswon')

returns the same error ('Your Firefox profile cannot be loaded. It may be missing or inaccessible.').

Jonathan Gerdes
  • 101
  • 1
  • 6

3 Answers3

4

tl;dr: Reinstalling firefox (uninstalling the snap package and installing with apt) fixed the problem.

I took SiKings comment to heart and the only answer mentioned "If your Web Browser base version is too old uninstall the Web Browser through Revo Uninstaller and install a recent GA released version of the Web Browser." so I reinstalled firefox which subsequently fixed the problem.

The version did not change:

$ firefox -V
Mozilla Firefox 93.0

I believe this was most likely a permission problem caused by the dist upgrade.

Jonathan Gerdes
  • 101
  • 1
  • 6
  • I believe the problem with the snap firefox version is Snap's security model, see: https://github.com/snapcore/snapd/wiki/Snap-Execution-Environment). Maybe there is a way to configure the apparmor profile for snap.firefox.firefox to allow loading the firefox profile from the path that was supplied in the Firefox startup parameter --profile , in my case (using Selenide test framework) this is in the /tmp directory. However, I haven't dived into this. – dschoorl Dec 30 '21 at 04:09
0
  1. Upgrade geckodriver to 0.32
  2. Create new profile from about:profiles tab in location outside of ~/snap. I created mine dir right in project directory, at first Firefox will put there only times.json.
  3. When hoisting FirefoxDriver addArguments:
     // code is for java, but you will get the idea ;)
     FirefoxOptions opt = new FirefoxOptions();

        opt.addArguments(
              "--profile", "/absolute/path/to/new/directory");
       
 // two lines to prevent: Invalid browser preferences for CDP err in log.
      opt.addPreference("fission.webContentIsolationStrategy", 0);
      opt.addPreference("fission.bfcacheInParent", false);

      WebDriver driver = new FirefoxDriver(opt);

Update: Couple of runs later Firefox had crushed!!!
Since I upgraded to Ubuntu 22.04 it happened from time to time. I have reinstalled it again with snap and decided to keep trying.
While I was checking for probable cause of this crush I spotted that I have like a lot of non-terminal processed with geckodriver. So I pidof geckodriver to see how all there were like six of them, then pkill -f geckodriver and later added code below after closing driver:

import java.lang.Process;
import java.lang.ProcessBuilder;
import java.util.concurrent.TimeUnit;


      driver.close();
// new addition
      String[] cli_args = new String[] {"/bin/bash", "-c", "pkill -f geckodriver"};
      try{
      Process proc = new ProcessBuilder(cli_args).start();
      proc.waitFor(1000, TimeUnit.MILLISECONDS);
      } catch (Exception e) {
          e.printStackTrace();
      }

Sorry for a lot of Java's code on Python's thread. If you know relevant thread for Java let me know, please.

dobhareach
  • 176
  • 3
  • 6
-1

Download web driver for firefox : https://github.com/mozilla/geckodriver/releases

then unzip file on default directory

tar -C /usr/local/bin/ -xvf geckodriver-v0.30.0-linux64.tar.gz

Add the chosen geckodriver directory to PATH

export PATH=$PATH:/YourDirectory

Create python file main.py

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://google.com/')

run python main.py

Try this way may be help you

Kayes Fahim
  • 672
  • 5
  • 16
  • 1
    The OP specified using geckodriver 0.30.0 (latest). Are you suggesting he **downgrade** his version? – SiKing Oct 25 '21 at 18:01
  • I didn't mean that I just wrote what I did. He can use higher version – Kayes Fahim Oct 25 '21 at 18:04
  • Downgrading to any other version does not work. I suspect it is a permission problem with the temporary profiles, but those "look" clean too (see edit 2). – Jonathan Gerdes Oct 25 '21 at 18:47
  • Ran into a similar issue today, no upgrade though. My Firefox was also installed through the store and not apt. Following posters steps solved the issue. I don't see what this has to do with geckodriver? – Just_Stacking Feb 14 '22 at 07:25