最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - SessionNotCreatedException when launching Selenium ChromeDriver with FastAPI Slack bot on AWS Lightsail - Stack Overflo

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

The quick fix:
Use tmux new-session -d -s mysession
Et VOILA !

What I tried before that:

  1. 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,  
    
  2. Checked for zombie ChromeDriver processes and kill them with:

    ps aux | grep chromedriver  
    
  3. Tried various Chrome options, including:
    chrome_options.add_argument("--no-sandbox")

    ...and several others

  4. 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论