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
You can try setting the download location using
--user-data-dir
anddownload.default_directory
instead ofoptions.add_experimental_option
remove all the
options.add_experimental_option
code and instead addThis might fix your problem and allow you to set the Download directory on your EC2
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 useos.mkdir()
if this is what breaks it.An easier way to do this altogether, though, is to just use
--user-data-dir
anddownload.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 likesudo chown -R ubuntu:ubuntu /home/ubuntu/resolucion