Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Chrome headless to download files? #679

Closed
rsshilli opened this issue Feb 7, 2018 · 15 comments
Closed

How to get Chrome headless to download files? #679

rsshilli opened this issue Feb 7, 2018 · 15 comments

Comments

@rsshilli
Copy link

rsshilli commented Feb 7, 2018

How do I download files using Chrome headless? I see that:

I'm having a hard time piecing together how I get this working from my Chimp world.

Versions:

  • Chimp: 0.50.2
  • Node.js: v6.10.3
  • Java: 1.8.0_121
  • Operation system: Windows 10

I've tried a number of things playing with the configuration, trying things like this below, but nothing works:

chimpOptions.webdriverio.desiredCapabilities = {
  chromeOptions: {
    args : ["--no-sandbox", "--headless", "--disable-gpu", "--window-size=1050,900"],
    prefs: {
      "credentials_enable_service": false,
      "Browser.setDownloadBehavior": {behavior : "allow", downloadPath: path.join((process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE), "Downloads")},
      "profile": {
        "password_manager_enabled": false,
        "default_content_setting_values": { "automatic_downloads": 1 }
      }
    }
  },
};

My mechanism for downloading a file is to click on the download link and then use fs to wait for the file on the file system, which never shows up :(. This worked fine before adding --headless

@ybatsiun
Copy link

I am facing the same issue, were you been able to solve it?

@ivoneijr
Copy link

Same issue.

@pelly
Copy link

pelly commented May 24, 2018

Any progress on this? I'm having the same issue.

@rsshilli
Copy link
Author

rsshilli commented May 24, 2018

I have this working, but I did it without the "--headless" mode. I was previously under the impression that the only way to make Chimp test Chrome in a docker container was the use the headless mode. I was wrong. Just remove the headless and everything works fine, even when in a docker container or running on a machine without a monitor.

@HolyWalley
Copy link

@rsshilli did you ever get an error message like this

unknown error: DevToolsActivePort file doesn't exist (Driver info: chromedriver=2.39.562737 (dba483cee6a5f15e2e2d73df16968ab10b38a2bf),platform=Linux 4.4.0-1049-aws x86_64)

when you run not headless chrome in docker-container?

@pedymaster
Copy link

@rsshilli
Iam interested in this too. I have the same error as HolyWalley when removing the headless flag

@rsshilli
Copy link
Author

rsshilli commented Jun 4, 2018

You have to pass in the full path to the file, right? It looks like from the error message that you just passed in the filename itself (DevToolsActivePort). The browser won't know where to find it.

@wuthiago
Copy link

I am also facing this problem. Anyone able to make it work? Thanks

@rsshilli
Copy link
Author

@wuthiago I made it work. Are you using the full path for the filename?

@wuthiago
Copy link

@rsshilli were you able to download files with the "--headless" mode on?

@rsshilli
Copy link
Author

rsshilli commented Jul 19, 2018 via email

@komalanandpandey
Copy link

Hi,
did any one get luck on downloading file using "headless chrome" , please help! Regards!

@pelly
Copy link

pelly commented Jul 19, 2018 via email

@c20xh2
Copy link

c20xh2 commented Jul 20, 2018

@komalanandpandey @wuthiago

Here's a solution that work for me :

Setting the driver options:

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
prefs = {'download.default_directory' : out_path}
options.add_experimental_option('prefs', prefs)

download_path = './output/'

driver = webdriver.Chrome('./chromedriver', chrome_options=options)

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

Downloading a file:

driver.get(url)
time.sleep(20)

IMPORTANT : You have to call time.sleep(x) to let some time for the download to finish, if the driver close before the end of file download, solution won't work

@samhatoum
Copy link
Member

This issue is being tracked in Chimpy
https://github.com/TheBrainFamily/chimpy/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants