0

Using Selenium Java in my automation framework and trying to download PDF from Chrome but specify name of that file is there an existing option?

here is my code:

System.setProperty("webdriver.chrome.driver", "resources/drivers/chromedriver.exe");
    
    ChromeOptions options = new ChromeOptions();
    
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("plugins.always_open_pdf_externally", true);
    chromePrefs.put("download.default_directory", "C:");
    options.setExperimentalOption("prefs", chromePrefs);
    
    driver = new ChromeDriver(options);
Ihor Harmatii
  • 189
  • 3
  • 21

1 Answers1

0

Found solution in this topic Selenium give file name when downloading answer from @supputuri It is not possible to give the name to downloaded file, but below is the code how to get the name of downloaded file.

public String waitUntilDonwloadCompleted(WebDriver driver) throws InterruptedException {
  // Store the current window handle
  String mainWindow = driver.getWindowHandle();
  
  // open a new tab
  JavascriptExecutor js = (JavascriptExecutor)driver;
  js.executeScript("window.open()");
 // switch to new tab
// Switch to new window opened
  for(String winHandle : driver.getWindowHandles()){
      driver.switchTo().window(winHandle);
  }
 // navigate to chrome downloads
  driver.get("chrome://downloads");
  
  JavascriptExecutor js1 = (JavascriptExecutor)driver;
  // wait until the file is downloaded
  Long percentage = (long) 0;
  while ( percentage!= 100) {
      try {
          percentage = (Long) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value");
          //System.out.println(percentage);
      }catch (Exception e) {
        // Nothing to do just wait
    }
      Thread.sleep(1000);
  }
 // get the latest downloaded file name
  String fileName = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text");
 // get the latest downloaded file url
  String sourceURL = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').href");
  // file downloaded location
  String donwloadedAt = (String) js1.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div.is-active.focus-row-active #file-icon-wrapper img').src");
  System.out.println("Download deatils");
  System.out.println("File Name :-" + fileName);
  System.out.println("Donwloaded path :- " + donwloadedAt);
  System.out.println("Downloaded from url :- " + sourceURL);
 // print the details
  System.out.println(fileName);
  System.out.println(sourceURL);
 // close the downloads tab2
  driver.close();
 // switch back to main window
  driver.switchTo().window(mainWindow);
  return fileName;

}

Ihor Harmatii
  • 189
  • 3
  • 21