Home > Back-end >  selenium java - how to download a pdf and save with a different name?
selenium java - how to download a pdf and save with a different name?

Time:09-21

As part of a work on an existing selenium test pack, I was asked to remove the instantiation of Firefox to download server-generated PDF documents - an authentication issue led to an agent config session error that prevented the documents from being downloaded.

When the document is generated in the server, it can be accessed via a URL that contains a UUID. This URL was passed to a fresh instance of Firefox and the document could be downloaded.

This selenium pack was thrown together NOT following the best practices of software development some 10 years ago. Though it worked, for the most part, its performance is quite poor.

I can access the generated document on the server with the service URL plus the UUID as part of the path, as this is part of the document generation process. In the current workflow, there is a 'generate document' button that is clicked by the running pack.

Once this document is generated, I need to download it to a specific folder and rename it to serve as proof the process has been completed to satisfaction.

I searched a lot and found a few articles that gave me insights into parts of the issue, but could not put together a working solution.

I can start chrome driver in headless mode but haven't figured out how to download the document. Since the browser window displays the document itself, there is no clickable download button on the page.

Can anyone point me to a solution here?

Thanks in advance,

CodePudding user response:

Currently while opening pdf in chrome we can see the download option but through selenium we cannot perform any actions on that download button. Here our goal is to download the pdf into desired location so we need to disable the pdf plugins before launching the driver. Please see the below code,

Required ChromOptions:

   ChromeOptions options = new ChromeOptions();
   HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
       chromeOptionsMap.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
       chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
       chromeOptionsMap.put("download.default_directory", "C:\\Users\\Downloads\\test\\");
       options.setExperimentalOption("prefs", chromeOptionsMap);
       options.addArguments("--headless");
  

Accessing PDF:

driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
driver.get("C:\\Users\\Downloads\\Bill.pdf");

Explanation:

plugins.plugins_disabled --> Disables viewing pdf in chrome.

plugins.always_open_pdf_externally --> Downloads the pdf on launching the respective link or URI.

download.default_directory --> Default download location can be changed.

CodePudding user response:

There is no way to do it using plain Selenium, but try to use AutoIT for it:

https://www.lambdatest.com/blog/how-to-download-upload-files-using-selenium-with-java/

  • Related