Save file dialog when trying to download file in selenium chrome

Question:

I am using selenium grid in docker. My nodes are created from selenium/node-chrome:4.8.0 image and my hub is created from selenium/hub:4.8.0 image
when I try to download a file with code below, google chrome shows a dialog asking for download path.

from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException
from selenium.webdriver.common.by import By
from time import sleep

options = webdriver.ChromeOptions()
url = "http://localhost:4444/wd/hub"
options.add_argument("--window-size=1920,1080")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--disable-notifications")
prefs = {"profile.managed_default_content_settings.images": 2,
         "download.default_directory": "/home/seluser/download",
         "download.prompt_for_download": False,
         "download.directory_upgrade": True,
         "plugins.always_open_pdf_externally": True,
        }
options.add_experimental_option("prefs", prefs)
driver = webdriver.Remote(command_executor=url,
                          options=options)
driver.maximize_window()
driver.get("https://dornsife.usc.edu/assets/sites/298/docs/ir211wk12sample.xls")
sleep(5)

screen shot of my session

My code was working in standalone selenium containers created from selenium/standalone-chrome:3.141.59 image.

Asked By: Yasin Arabi

||

Answers:

Finally after hours of searching to find an answser without any result, I tried an alternative way.

Why I used selenium to download files? because I need to log in first then try to download files.

In this method after logging in I load selenium cookies in requests session then download files with requests.

def download_file(driver, url: str, dest_path: str):
    s = requests.Session()
    for cookie in driver.get_cookies():
        s.cookies.set(cookie['name'], cookie['value'])
    r = s.get(url, stream=True)
    with open(dest_path, "wb") as f:
        for chunk in r.iter_content(chunk_size=1024):
            f.write(chunk)
Answered By: Yasin Arabi