1

I want to download a file and I am able to do it with the code below. When I pass options to the driver, download does not start.

from selenium import webdriver
url = "http://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx"
driver.get(url)
driver.find_element_by_id("btnDownload").click()

I tried to pass following options, but download does not start:

from selenium import webdriver
url = "http://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx"
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=H:/")
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")
with webdriver.Chrome(chrome_options=options) as driver:
    driver.get(url)
    driver.find_element_by_id("btnDownload").click()

I also tried:

from selenium import webdriver
url = "http://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx"
with webdriver.Chrome() as driver:
    prefs = {
    "download.default_directory": down_dir,
    "download.prompt_for_download": False,
    "download.directory_upgrade": True
    }

    options.add_experimental_option('prefs', prefs)
    driver.get(url)
    driver.find_element_by_id("btnDownload").click()

I would like to download the file with hidden browser window. Also, is there a way to close it just after successful download (using driver.quit())?

EDIT:

I removed duplicated driver instances - error that I made during copying pieces of code.

Sliwa
  • 61
  • 8
  • 1
    not sure why you're using "options = options" there, try with ".Chrome(options)". You can use your own code to confirm if the file exists before closing the driver. Should return false if it's still downloading. (It'll be named as a ".part" file before done...) – pcalkins Jun 26 '19 at 18:55
  • "options = options" is a good practice as that initializer accepts a bunch of named arguments. – ipaleka Jun 26 '19 at 21:04
  • Check this If you want to [make sure the download is completed before closing the driver](https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading/56570364#56570364). – supputuri Jun 27 '19 at 02:39
  • Possible duplicate of [Downloading with chrome headless and selenium](https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium) – Chris B. Jun 28 '19 at 17:11
  • @ChrisB. this is the same issue, but suggested solution does not work. – Sliwa Jun 29 '19 at 19:32
  • Does this [discussion](https://stackoverflow.com/questions/57599776/download-file-through-google-chrome-in-headless-mode/57606294#57606294) helps you? – undetected Selenium Aug 22 '19 at 10:00

1 Answers1

1

You instantiate your webdriver twice, delete/comment out the other line:

with webdriver.Chrome(options=options) as driver:
    # driver = webdriver.Chrome(options=options)

Also not sure for Options class, I believe you should import it with:

from selenium.webdriver.chrome.options import Options

EDIT: yep, Chrome's not going to download in headless mode: SO answer.

So, the solution for you is:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url = "http://wwwapps.tc.gc.ca/Saf-Sec-Sur/2/CCARCS-RIACC/DDZip.aspx"


def enable_download_in_headless_chrome(driver, download_dir):
    # add missing support for chrome "send_command"  to selenium webdriver
    driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
    driver.execute("send_command", params)

options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
enable_download_in_headless_chrome(driver, "H:/")
driver.get(url)
driver.find_element_by_id("btnDownload").click()
ipaleka
  • 3,745
  • 2
  • 13
  • 33
  • Thanks for looking into this, it still does not download. – Sliwa Jun 27 '19 at 16:35
  • It works in my environment (GNU/Linux) with changed directory to "/home/username/"; Chrome 75.0.3770.100 (Official Build) (64-bit), ChromeDriver 75.0.3770.90 – ipaleka Jun 27 '19 at 16:57
  • Thanks, I'm on Windows 7. From your code I removed ```options.add_argument('--headless')``` to check what will happen and I received download error on Chrome. So I think there is a problem. The code works nicely with hidden browser so I'm almost there! – Sliwa Jun 27 '19 at 17:46
  • I updated chromedriver to your version, still won't download. – Sliwa Jun 29 '19 at 19:22