gogoogle-chrome-headlesschromedp

Headless chromedp wait until download finishes


I'm using chromedp to navigate thru the website, to download PDF files which are generated by the system. It takes a while, to generate them so... Code looks like that:

        chromedp.Navigate("https://website.com/with/report/to/download"),
        // wait for download link
        chromedp.WaitReady("a.downloadLink"),
        chromedp.Click("a.downloadLink"),
        // wait some time to pull the file
        chromedp.Sleep(time.Minute),
        chromedp.Click("#close-button"),

Right now I'm waiting a minute, and then close the browser, but I don't like it that way. If there is any way to control or get some kind of "event" when file download is finished?


Solution

  • This question was asked a long time ago, but since nobody answered and the entire chromedp tag bunch of questions is kind of thin with answers... here goes.

    You should not depend on the passage of time for any asynchronous operations. See the chromedp file download example for the correct way to download a file. Note how they synchronize on download end.