1

I am using chromedriver to download a file. It works pretty fine without headless parameter. But when I try to do the same in a headless way I get the following error:

every_downloads_chrome failed: Message: javascript error: downloads is not defined
(Session info: headless chrome=76.0.3809.87)

This is the piece of code I am using to be able to download the file:

def open(self) -> None:
    # Enable verbose logging
    service_args = ["--verbose", "--log-path={}/chromedriver.log".format(self.download_dir)]
    self.browser = webdriver.Chrome(chrome_options=self.options, service_args=service_args)

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

This is the function I call to check if file was downloaded:

def every_downloads_chrome(self, driver):
    if not driver.current_url.startswith("chrome://downloads"):
        driver.get("chrome://downloads/")

    return driver.execute_script("""
        var items = downloads.Manager.get().items_;
        if (items.every(e => e.state === "COMPLETE"))
            return items.map(e => e.file_url);
        """)

Attempt to download the file:

driver.execute_script("__doPostBack('...','')")
time.sleep(10)

WebDriverWait(driver, 600, 30).until(self.every_downloads_chrome)

The error is coming from driver.execute_script call inside every_downloads_chrome function.

Does anyone have any idea? I also tried to remove every_downloads_chrome_ and just include some time.sleep(120), but the file is not being downloaded (if it helps - they have ~10MB).

Thank you!

briba
  • 2,857
  • 2
  • 31
  • 59
  • The API was updated. Have a look at https://stackoverflow.com/questions/48263317/selenium-python-waiting-for-a-download-process-to-complete-using-chrome-web/48267887#48267887 – Florent B. Jul 31 '19 at 17:02
  • @FlorentB. using this function (similar to the one I posted) I still get "javascript error: downloads is not defined" :( – briba Aug 05 '19 at 10:57
  • looks like the downloads page is missing when chrome is launched heedlessly. – Florent B. Aug 05 '19 at 11:24
  • @FlorentB. Exactly! But actually my mistake here. I was using the method I posted and not the method from the link you posted. I used this one, there was no errors but nothing happened at the end (there was no file) oO – briba Aug 05 '19 at 11:58
  • Just callind like this: "WebDriverWait(driver, 600, 30).until(self.every_downloads_chrome)" – briba Aug 05 '19 at 12:01

0 Answers0