I have a FastAPI server with a Slack chatbot that launches Selenium for automating certain tasks. However, I'm encountering an issue when initializing the ChromeDriver with the --user-data-dir
argument:
user_data_dir = tempfile.mkdtemp()
chrome_options.add_argument(f"--user-data-dir={user_data_dir}")
# Initializing the driver
driver = webdriver.Chrome(options=chrome_options)
When I run this, it fails with the following error:
seleniummon.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
When I connect on ssh to the server it works without doing nothing.
I tried change the path:
chrome_options.add_argument("--user-data-dir=/home/ubuntu/mytmp/")
I tried static path
chrome_options.add_argument("--user-data-dir=/home/ubuntu/mytmp/")
And I have always the same behaviour.
I use AWS Lightsail with Ubuntu 22.04.5 LTS
I have a FastAPI server with a Slack chatbot that launches Selenium for automating certain tasks. However, I'm encountering an issue when initializing the ChromeDriver with the --user-data-dir
argument:
user_data_dir = tempfile.mkdtemp()
chrome_options.add_argument(f"--user-data-dir={user_data_dir}")
# Initializing the driver
driver = webdriver.Chrome(options=chrome_options)
When I run this, it fails with the following error:
seleniummon.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
When I connect on ssh to the server it works without doing nothing.
I tried change the path:
chrome_options.add_argument("--user-data-dir=/home/ubuntu/mytmp/")
I tried static path
chrome_options.add_argument("--user-data-dir=/home/ubuntu/mytmp/")
And I have always the same behaviour.
I use AWS Lightsail with Ubuntu 22.04.5 LTS
Share Improve this question asked Mar 28 at 17:39 mioreymiorey 9568 silver badges20 bronze badges1 Answer
Reset to default 0The quick fix:
Use tmux new-session -d -s mysession
Et VOILA !
What I tried before that:
AppArmor tweaks – ChromeDriver (installed via Snap) might be restricted by AppArmor. I added these rules to
/var/lib/snapd/apparmor/profiles/snap.chromium.chromedriver
:allow /tmp/** rw, allow /run/user/** rw, allow /dev/shm/** rw,
Checked for zombie ChromeDriver processes and kill them with:
ps aux | grep chromedriver
Tried various Chrome options, including:
chrome_options.add_argument("--no-sandbox")
...and several others
Attempted to start a TTY during Python execution
The solution:
The tmux
session is triggered via SSH through my CI/CD pipeline to ensure a session remains continuously open on the server. This setup bypasses the restrictions related to directory creation and usage due to the environment settings. My API isn't executed within the tmux environment, but tmux keeps the session alive, allowing the process to run without interference.