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!