Playwright button/link click change the url to pdf file on browser, how to save that pdf to disk?

324 Views Asked by At

In my Java Playwright script (code below), the download click on the link, changes the browser URL to the PDF file's path and the resulting PDF loads on the chromium browser.

I tried using the following code to save the resulting PDF to the disk, but get a timeout at the waitForDownload itself. However the link click there seem to work as I observe the PDF is loaded onto the browser.


    Download download = page.waitForDownload(new Runnable() {
        @Override
        public void run() {
            page.locator("#pdflink").click();
        }
    });

    download.saveAs(Paths.get("./", pdfFileName));

How can I save the loaded PDF (now that page URL is changed to the PDF and the chromium now showing the PDF!) to the disk?

I tried going headless mode and saving the PDF, without success.

byte[] pdf = page.pdf();
Files.write(Path.of(pdfFile), pdf);

I found a similar question: Can't download file with Playwright Chromium or Webkit

But I think the primary issue I am having could be due to the website behaviour change for chromium to embed the pdf while webkit tend to download the file.

ie:

<html><head></head><body style="height: 100%; width: 100%; overflow: hidden; margin:0px; background-color: rgb(82, 86, 89);"><embed name="A950021BBBDE7B4177BC65915B78E8D9" style="position:absolute; left: 0; top: 0;" width="100%" height="100%" src="about:blank" type="application/pdf" internalid="A950021BBBDE7B3177AC65915B78E8D9"></body></html>
0

There are 0 best solutions below