4

I have written a (Python) script using Selenium to automate some downloads from Box (I'm aware there is a Box API, but I'm accessing school resources and I'm not sure that the API would interact well with the school's external authentication). The script works using the Chrome driver in non-headless mode, but not in headless.

The step where I'm having issues is an attempt to query the status of downloads. I think my script is actually able to execute the downloads correctly: I've added some code from SO to disable security defaults that prevent downloading in headless mode.

I've successfully queried the download status using two methods (both executing Javascript in Chrome):

Both stop working when I switch to headless mode.

Here's the actual code I'm using:

# Headless Chrome disables downloads by default (at least as of 2017).
# https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium
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},
    }
    return driver.execute("send_command", params)


def get_top_download_state(driver):
    """Call this after running driver.get("chrome://downloads")."""

    [state, percent, progress] = driver.execute_script("""
    var item = downloads.Manager.get().items_[0];
    var state = item.state;
    var percent = item.percent;
    var progress = item.progressStatusText;
    return [state, percent, progress];
    """)

    return state, percent, progress

Errors for the first method say that downloads is not defined. Errors for the second method say that you can't get the shadowRoot property of null (presumably the query selectors are just finding nothing).

dysonsfrog
  • 155
  • 1
  • 6
  • You need to use ChromeDriver 77.0.3865.10/Chrome 77 . `Support to save file downloads in headless mode` - ChromeDriver 77.0.3865.10 https://chromedriver.chromium.org/downloads – Rahul L Sep 04 '19 at 03:44
  • Hi @dysonsfrog. Could you make it? Im passing through same error and solutions. I can't find a solution for headless mode. Thank you – Javier Cornejo Alfaro Nov 04 '19 at 23:53
  • 1
    @JavierCornejoAlfaro Unfortunately I can't help you. Eventually the script just finished running without headless mode and I stopped looking for a solution. – dysonsfrog Nov 05 '19 at 17:47
  • 1
    Same problem here. Everything works on normal mode, but on headless mode I get: "selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'shadowRoot' of null" – Julio Batista Silva May 08 '20 at 19:32
  • Same issue here when I try in headless mode. Working perfectly otherwise. – jbright May 10 '20 at 00:02
  • any luck till date? – Shaun Henry Oct 18 '20 at 03:01
  • @ShaunHenry, not for me, no. As mentioned earlier I lost my need to figure it out. – dysonsfrog Oct 18 '20 at 22:49

0 Answers0