5

I'm trying to download a file with selenium. I've searched everything.

At How to control the download of files with Selenium Python bindings in Chrome some people told that it worked. But it didn't worked for me! Maybe I miss something? The only things differentlly is that my page autostarted download the csv file.

After studying the chrome codes I added:

        "safebrowsing_for_trusted_sources_enabled": False

But still id didn't worked.

options = Options()
options.add_argument("--disable-notifications")
options.add_argument('--no-sandbox')
options.add_experimental_option("prefs", {
    "download.default_directory": "C:\\Users\\claudiu.ivanescu\\Downloads",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing_for_trusted_sources_enabled": False
})
options.add_argument('--disable-gpu')
options.add_argument('--disable-software-rasterizer')
options.add_argument('--headless')

Thank for support

Claudiu
  • 577
  • 1
  • 9
  • 24

1 Answers1

25

If anybody interested, after 2 days of search :). I manage to make it works!

I found the answer the bug tracking in this comment: https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c86

The code I used is:

def enable_download_headless(browser,download_dir):
    browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
    browser.execute("send_command", params)

if __name__ == '__main__':
    options = Options()
    options.add_argument("--disable-notifications")
    options.add_argument('--no-sandbox')
    options.add_argument('--verbose')
    options.add_experimental_option("prefs", {
        "download.default_directory": "C:\\tmp",
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing_for_trusted_sources_enabled": False,
        "safebrowsing.enabled": False
    })
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-software-rasterizer')
    options.add_argument('--headless')
    driver_path = "C:\\Users\\tmp\\chromedriver.exe"
    driver = webdriver.Chrome(driver_path, chrome_options=options)
    enable_download_headless(driver, "C:/tmp")
    driver.get(url)

Maybe will be some use to others in the future... Probably is a lot of useless things inside, but didn't have time yet to change :).

Matheus Araujo
  • 5,551
  • 2
  • 22
  • 23
Claudiu
  • 577
  • 1
  • 9
  • 24
  • 1
    Hey, I tried to run your code, it did not produce any error but did not download the file either. After calling `enable_download_headless(driver, ".")`, I did `btn_dl.click()` but it did nothing. – Volatil3 Feb 02 '21 at 13:33
  • Selenium it's a difficult thing. It worked for me with the chromedriver from 2 years ago. You have to check the compatibilities between versions. – Claudiu Feb 04 '21 at 07:30
  • 1
    Voodoo magic, but a working voodoo magic. – Sida Zhou Feb 25 '21 at 08:22
  • 1
    Tried so many configurations of these arguments and couldn't get the download to work until I found your post. Thank you! This may have been what I needed: `safebrowsing_for_trusted_sources_enabled": False` – JoeVorbs Apr 14 '21 at 13:42
  • 1
    Hai @Claudiu - after a long search this works for me and saves my time. – kta Jun 16 '21 at 05:51
  • 1
    @Claudiu, I must extra login to SO to thank you for this. Thank you. Still work as of 01Nov2022. – Đức Thanh Nguyễn Nov 01 '22 at 04:33