[Python] How can I correctly run a selenium standalone-chrome container with a different user...

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to work with python and selenium/standalone-chrome to create an automation to download a .xlsx file from my website.
    I've configured both the python container and the selenium/standalone-chrome one to work on the same network and share the same docker volume.

    docker run -d -v selenium_vol:/home/myuser/Downloads -p 4444:4444 -p 7900:7900 --name download_cont --network test_net --shm-size="2g" selenium/standalone-chrome

    docker run -d -v selenium_vol:/usr/src/app/download_folder --network test_net --name python_auto automation


    I've noticed that, when the permission of the local file system folder associated with selenium_vol are 777, the selenium container correctly download the documents, otherwise it says that it need permissions to download the file.
    To avoid setting my volume with 777 permission, I've tried to setup the selenium container with a user that matches the one on my local machine, that owns the folder, in order to being able to download the file without problem.

    The selenium custom docker file looks like this

    FROM selenium/standalone-chrome:latest

    # Adding the group and the user for security reinforcement purpose
    RUN sudo groupadd -o --gid 1000 myuser
    RUN sudo useradd -o --uid 1000 --gid myuser --shell /bin/sh --create-home myuser

    # Adding the folder in which the program will be copied and called
    RUN sudo mkdir /home/myuser/Downloads
    RUN sudo chown -R myuser:myuser /home/myuser/Downloads
    RUN sudo chmod 777 /home/myuser/Downloads

    # Changing directory
    WORKDIR /home/myuser/Downloads

    # Copying all the files, and changing the ownership to the new user, myuser
    COPY --chown=myuser:myuser . .

    # Changing the user to the one we created before, with limited access to the machine: myuser
    USER myuser


    But when i try to the container with the following python code

    options = webdriver.ChromeOptions()

    options.add_argument('--no-sandbox')
    options.add_argument("--disable-dev-shm-usage")

    prefs = {
    "download.default_directory": "/home/myuser/Downloads/",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True }

    driver = webdriver.Remote(command_executor="http://localhost:4444", options=options)

    try:

    driver.set_page_load_timeout(120)
    driver.set_script_timeout(120)
    driver.implicitly_wait(120)


    print(driver.session_id)

    driver.command_executor._commands["send_command"] = (
    "POST", '/session/' + driver.session_id + '/chromium/send_command')

    params = {'cmd': 'Page.setDownloadBehavior',
    'params': {'behavior': 'allow', 'downloadPath': '/home/myuser/Downloads/'}}
    command_result = driver.execute("send_command", params)

    my_function(driver)
    driver.quit()
    except Exception as e:
    print(e)
    driver.quit()


    Wha I obtain as a response is

    Traceback (most recent call last):
    File "Drive:\my_automation_test\main.py", line 149, in <module>
    driver = webdriver.Remote(command_executor="http://localhost:4444", options=options)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "Drive:\my_automation_test\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 208, in __init__
    self.start_session(capabilities)
    File "Drive:\my_automation_test\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 292, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "Drive:\my_automation_test\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
    File "Drive:\my_automation_test\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.SessionNotCreatedException: Message: Could not start a new session. No nodes support the capabilities in the request
    Host info: host: 'host-id', ip: 'xxx.xxx.xxx.xxx'
    Build info: version: 'ver', revision: 'revision-id'
    System info: os.name: 'Linux', os.arch: 'amd64', os.version: 'os-version', java.version: '17.0.12'
    Driver info: driver.version: unknown
    Stacktrace:
    at org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.checkMatchingSlot (LocalDistributor.java:816)
    at org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.run (LocalDistributor.java:798)
    at org.openqa.selenium.concurrent.GuardedRunnable.lambda$guard$0 (GuardedRunnable.java:35)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:539)
    at java.util.concurrent.FutureTask.runAndReset (FutureTask.java:305)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run (ScheduledThreadPoolExecutor.java:305)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:635)
    at java.lang.Thread.run (Thread.java:840)

    Process finished with exit code 1


    I've tried to connect whit the default options (with all the code above commented), but nothing changes. I've tried looking on internet for some best practices to use to run standalone-chrome with a different user than seluser but I didn't find nothing that can help me. I also tried to configure the default /home/seluser/Downloads but the result is still the same.

    How can I proceede? There a correct way to run a selenium container with a different user or should I make another solution?

    Thank you!

    Continue reading...

Compartilhe esta Página