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)
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 libasound2My full steps for the DO ubuntu server.
For this codeā¦
No issues.
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 libasound2t64fixed the issue for ubuntu 24.10, i changedlibasound2tolibasound2t64.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.Chromeorwebdriver.Edge, and a slightly different failure withwebdriver.Firefox.The repro steps are simply:
$ sudo suThen run a Python script containing:
Here is a Python shell session showing all 3 failures:
Here is what happens when trying to just run Chrome as root:
Here is what happens when trying to run Chrome as root and including the
--no-sandboxoption:Here is what happens when trying to run Firefox as root:
I definitely have an X server running⦠and here is the contents of $DISPLAY:
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
chromedriveris installed:chromedriver --versionIf not install with:apt install chromium-chromedriverIf you are using ubuntu bash, u have to run this commend before runing a selenium program
sudo apt install -y libxss1 libappindicator3-1 libindicator7and 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 usingchrome-linux64.zipfromchrome-for-testing, in package there is a filedeb.depswith 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:
As root:
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.