1
        FirefoxOptions options = new FirefoxOptions();
        FirefoxProfile profile = new FirefoxProfile();

        // Accept Untrusted Certificates
        profile.setAcceptUntrustedCertificates(true);
        profile.setAssumeUntrustedCertificateIssuer(false);

        //Directly download PDF


        profile.setPreference("browser.helperApps.neverAsk.openFile", "application/pdf");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
        profile.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer

        // Set Firefox profile to capabilities
        options.setCapability(FirefoxDriver.PROFILE, profile);

I have used above code to configure firefox so that it directly downloads the PDF when clicked on a PDF link using selenium webdriver. When an automation script clicks on a link that download the PDF, the script downloads the file directly but it loses the original name but downloaded as 'Document' refer attachment for screenshot of downloaded files. enter image description here

user7645022
  • 53
  • 1
  • 1
  • 7
  • Does this answer your question? [Selenium problems with PDF download in Firefox](https://stackoverflow.com/questions/52208798/selenium-problems-with-pdf-download-in-firefox) – Greg Burghardt Apr 03 '20 at 11:11

1 Answers1

0

The solution to this problem is http download file name — the web server needs to send certain HTTP headers back to the client to set the file name. You won't fix this with a browser setting, or anything to do with Selenium.

Fixing this requires setting the content-disposition HTTP header in the response from the server. Something like this:

content-disposition: attachment; filename=TheFileNameYouWant.pdf
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Hi Greg, Thanks for your reply. I need to point out that same link works fine in chrome. It gives proper name which URL has. I see below header HTTP/1.1 200 200 Date: Fri, 03 Apr 2020 05:46:47 GMT Content-Disposition: inline; filename=MANDATE-REFERENCE000000000000000268.pdf Keep-Alive: timeout=15, max=98 Connection: Keep-Alive Content-Type: application/pdf Transfer-Encoding: chunked – user7645022 Apr 03 '20 at 05:51
  • I checked Firefox manually as well (i.e. without preference set by selenium geckodriver.) The header contains content-disposition. Also, from about:config I set pdfjs.disabled", true browser.helperApps.neverAsk.openFile", "application/pdf" browser.helperApps.neverAsk.saveToDisk", "application/pdf" then also it works fine. – user7645022 Apr 03 '20 at 06:17