selenium: [šŸ› Bug]: Selenium SessionNotCreatedException: "User data directory is already in use"

What happened?

I am trying to launch a headless Chrome WebDriver using Selenium in Python. However, I keep encountering this error:

How can we reproduce the issue?

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

def createModel():
    service = Service(ChromeDriverManager().install())
    options = webdriver.ChromeOptions()
    
    # Specifying user data directory
    options.add_argument("--user-data-dir=/root/video-downloader/tmp/selenium-user-data")  
    options.add_argument("--headless=new")
    options.add_argument("--start-maximized")

    driver = webdriver.Chrome(service=service, options=options)
    return driver

driver = createModel()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()

Relevant log output

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: probably user data directory is already in use, 
please specify a unique value for --user-data-dir argument, or don't use --user-data-dir

Operating System

Ubuntu

Selenium version

4.29.0

What are the browser(s) and version(s) where you see this issue?

Chrome

What are the browser driver(s) and version(s) where you see this issue?

Google Chrome 133.0.6943.126 ChromeDriver stable(auto)

Are you using Selenium Grid?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 10
  • Comments: 54 (24 by maintainers)

Commits related to this issue

Most upvoted comments

It also occurs in my environment. I am running my tool with administrator privileges. This error does not occur except when running as administrator. I assume that an Edge version update triggers this error.

OS:Windows10 language:c#

@cgoldberg I spun up another ubuntu DO droplet. It appears that this item below ā€œfixedā€ the issue. I ran everything as root user, too but will not run as root going forward 😃

sudo apt install libgtk-3-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2

My full steps for the DO ubuntu server.

sudo apt update
sudo apt -y upgrade
sudo apt install -y python3-pip
sudo apt install -y python3-venv
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
sudo apt install libgtk-3-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2
python3 -m venv my_env
source my_env/bin/activate

For this code…

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

def load_website(url):
    options = Options()
    options.add_argument("--headless=new")  # Run in headless mode (no GUI)
    options.add_argument("--disable-gpu")
    options.add_argument("--no-sandbox")
    
    service = Service()
    driver = webdriver.Chrome(service=service, options=options)
    
    try:
        driver.get(url)
        print("Page Title:", driver.title)
    finally:
        driver.quit()

if __name__ == "__main__":
    load_website("https://example.com")

No issues.

(my_env) root@ubuntu-s-1vcpu-2gb-nyc1-01-test:/home/web# python test.py
Page Title: Example Domain

edit: this is only true for ubuntu 22.04. Steps above work for ubuntu 22.04.

None of the steps above worked for ubuntu 24.10, running as non-root user and installing the apts listed above. Still have the same issue for ubuntu 24.10

edit3: I’m going nuts, my brain is melting. It appears that sudo apt install libgtk-3-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2t64 fixed the issue for ubuntu 24.10, i changed libasound2 to libasound2t64.

I was able to reproduce this on Debian, but only running as root.

I get the same exception reported in this issue when trying to instantiate webdriver.Chrome or webdriver.Edge, and a slightly different failure with webdriver.Firefox.

The repro steps are simply:

$ sudo su

Then run a Python script containing:

from selenium import webdriver
driver = webdriver.Chrome()

Here is a Python shell session showing all 3 failures:

(venv) root@penguin:/home/cgoldberg617/code# python3
Python 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 66, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 250, in __init__
    self.start_session(capabilities)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
Stacktrace:
#0 0x5561c9c5614a <unknown>
#1 0x5561c96f3b80 <unknown>
#2 0x5561c972d03f <unknown>
#3 0x5561c9728d7f <unknown>
#4 0x5561c9779819 <unknown>
#5 0x5561c9778d46 <unknown>
#6 0x5561c976aca3 <unknown>
#7 0x5561c9736f08 <unknown>
#8 0x5561c9738071 <unknown>
#9 0x5561c9c1fb5b <unknown>
#10 0x5561c9c23ae2 <unknown>
#11 0x5561c9c0b967 <unknown>
#12 0x5561c9c246d4 <unknown>
#13 0x5561c9befc7f <unknown>
#14 0x5561c9c44cd8 <unknown>
#15 0x5561c9c44ea9 <unknown>
#16 0x5561c9c54fc6 <unknown>
#17 0x7f70603ee1c4 <unknown>

>>> driver = webdriver.Edge()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/edge/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 66, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 250, in __init__
    self.start_session(capabilities)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
Stacktrace:
#0 0x55d1fdf4114e <unknown>
#1 0x55d1fd9b1d46 <unknown>
#2 0x55d1fd9ed47f <unknown>
#3 0x55d1fd9e6a8d <unknown>
#4 0x55d1fda36a03 <unknown>
#5 0x55d1fda35b2f <unknown>
#6 0x55d1fda27273 <unknown>
#7 0x55d1fd9f4bb0 <unknown>
#8 0x55d1fd9f5841 <unknown>
#9 0x55d1fdf14a61 <unknown>
#10 0x55d1fdf17c7e <unknown>
#11 0x55d1fdf176fb <unknown>
#12 0x55d1fdf180a5 <unknown>
#13 0x55d1fdf06492 <unknown>
#14 0x55d1fdf1846c <unknown>
#15 0x55d1fdeef225 <unknown>
#16 0x55d1fdf31a28 <unknown>
#17 0x55d1fdf31c48 <unknown>
#18 0x55d1fdf4055c <unknown>
#19 0x7f2df31701c4 <unknown>

>>> driver = webdriver.Firefox()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/firefox/webdriver.py", line 71, in __init__
    super().__init__(command_executor=executor, options=options)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 250, in __init__
    self.start_session(capabilities)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "/home/cgoldberg617/code/venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 232, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1

>>> 

Here is what happens when trying to just run Chrome as root:

(venv) root@penguin:/home/cgoldberg617/code# ~/.cache/selenium/chrome/linux64/133.0.6943.141/chrome
[6856:6856:0227/140357.019460:ERROR:zygote_host_impl_linux.cc(100)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

Here is what happens when trying to run Chrome as root and including the --no-sandbox option:

(venv) root@penguin:/home/cgoldberg617/code# ~/.cache/selenium/chrome/linux64/133.0.6943.141/chrome --no-sandbox
No protocol specified

[6863:6863:0227/140411.630874:ERROR:ozone_platform_x11.cc(245)] Missing X server or $DISPLAY
[6863:6863:0227/140411.631036:ERROR:env.cc(257)] The platform failed to initialize.  Exiting.

Here is what happens when trying to run Firefox as root:

(venv) root@penguin:/home/cgoldberg617/code# ~/.cache/selenium/firefox/linux64/135.0.1/firefox
No protocol specified

Error: cannot open display: :0

I definitely have an X server running… and here is the contents of $DISPLAY:

(venv) root@penguin:/home/cgoldberg617/code# echo $DISPLAY
:0

So… I would say that this is not a bug in Selenium, ChromeDriver, EdgeDriver, or GeckoDriver. It is an issue with running Chrome/Edge/Firefox as root. I don’t know the cause. Perhaps a bug in the browsers? Perhaps it’s because root can’t open an X display running as a regular user?

Regardless, the real question is WHY do you need to run Selenium (and therefore the browser and driver) as root? I can’t think of a valid use case for this… but maybe someone has one.

Also, make sure chromedriver is installed: chromedriver --version If not install with: apt install chromium-chromedriver

If you are using ubuntu bash, u have to run this commend before runing a selenium program sudo apt install -y libxss1 libappindicator3-1 libindicator7 and add the folwoing arguments if you are using and ubuntu rdp: options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage")

@niceguy4, I saw your comment mentioned the issue when you ran test in the container, and fix by installing missing deps via apt install. I am not sure the browser pre-configure, but looks like you are using chrome-linux64.zip from chrome-for-testing, in package there is a file deb.deps with mention all deps needed for chrome can be launched, I think you can refer to that to keep container env install enough deps.

@niceguy4 That’s good to know… but that’s purely a Chrome dependency issue and not something Selenium developers can fix.

@cgoldberg Indeed, my Docker container is running with root privileges. After some debugging, I found that adding --user-data-dir was not the solution in my case, but using --no-sandbox was.

My issue seems to be related to running Chrome as root without the --no-sandbox option (see this issue).

When trying to run the Chrome command:

As non-root:

FATAL:zygote_host_impl_linux.cc(127)] No usable sandbox! If you are running on Ubuntu 23.10+ or another Linux distro that has disabled unprivileged user namespaces with AppArmor, see https://chromium.googlesource.com/chromium/src/+/main/docs/security/apparmor-userns-restrictions.md. Otherwise see https://chromium.googlesource.com/chromium/src/+/main/docs/linux/suid_sandbox_development.md for more information on developing with the (older) SUID sandbox. If you want to live dangerously and need an immediate workaround, you can try using --no-sandbox.

As root:

ERROR:zygote_host_impl_linux.cc(100)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

It looks like this issue is already being discussed here: https://issues.chromium.org/issues/373753919.

@cgoldberg Running it with administrator rights was a method I found while looking for a way to reproduce the error.

The PC where the error occurred did not have administrator rights, but it did occur. And I was able to avoid the error with this method.

Although it is a Chrome issue, I thought that a similar method could be used, so I left a comment.