skip to Main Content

With Selenium on my Ubuntu EC2 instance, I’m trying to set the download directory of Chrome to /home/ubuntu/resolucion, but the files downloaded are going to /home/ubuntu (where my script main.py is). I tried not using os and just typing "download.default_directory": "/home/ubuntu/resolucion" but still not working. The code below only works fine on my pc.

Note: there are no errors on the browser logs, neither any kind of error during script execution.

main.py

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

options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--headless")

dir_base = os.path.abspath(os.curdir)
carpeta_resolucion = os.path.join(dir_base, "resolucion")

options.add_experimental_option("prefs", {
    "download.default_directory": carpeta_resolucion,
    "browser.downloads.dir": carpeta_resolucion,
    "directory_upgrade": True,
    "download.prompt_for_download": False,
    "download.extensions_to_open": "application/xml",
    "safebrowsing.enabled": True
})

print(options.to_capabilities())

driver = webdriver.Chrome(service=Service(
    ChromeDriverManager().install()), options=options)

This is the printed information of options.to_capabilities():

{'browserName': 'chrome', 'pageLoadStrategy': 'normal', 'goog:chromeOptions': {'prefs': {'download.default_directory': '/home/ubuntu/resolucion', 'browser.downloads.dir': '/home/ubuntu/resolucion', 'directory_upgrade': True, 'download.prompt_for_download': False, 'download.extensions_to_open': 'application/xml', 'safebrowsing.enabled': True}, 'extensions': [], 'args': ['--no-sandbox', '--headless']}}

2

Answers


  1. You can try setting the download location using --user-data-dir and download.default_directory instead of options.add_experimental_option

    remove all the options.add_experimental_option code and instead add

    options.add_argument(f"--user-data-dir={dir_base}/user-data")
    options.add_argument(f"--download.default_directory={carpeta_resolucion}")
    

    This might fix your problem and allow you to set the Download directory on your EC2

    Login or Signup to reply.
  2. I see a few possible issues with this, firstly: are you sure that it is getting the path correctly?

    You may want to change these two lines to the full path directory (From your print, it looks like maybe '/home/ubuntu/resolucion') instead of a relative one. (Most IDEs will let you right click the path and copy it directly. Make sure that this directory actually exists and isn’t just a symbolic link to a non-existent directory. You can always add a check and use os.mkdir() if this is what breaks it.

    dir_base = os.path.abspath(os.curdir)
    carpeta_resolucion = os.path.join(dir_base, "resolucion")
    

    An easier way to do this altogether, though, is to just use --user-data-dir and download.default_directory instead.

    (Edit: just saw the other answer also mentioned this!)

    If both of those didnt work, the issue could be with your Linux permission group. If for example Ubuntu is the user, you could do something like sudo chown -R ubuntu:ubuntu /home/ubuntu/resolucion

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search