0

In my web application, I have to verify the PDF content. When I click the download link for the PDF, it opens a new tab with PDF opened and download icon to download the PDF(as shown in below screenshot). To compare and test my PDF I need to download the PDF. Can anyone guide me on how to click the download icon? enter image description here

I'm using Selenium and Java. Appreciate if anyone can guide on this issue.

I'm not sure on how to interact with the download icon

Divya
  • 15
  • 2

1 Answers1

0

Add following setExperimentalOption preference in you setup code for driver, When you run below code, instead of the loading the pdf it will directly download it in the location value provided for preference "download.default_directory" Also added a working code for a example website, where instead of opening a new window with PDF , pdf file will be downloaded directly

            WebDriverManager.chromedriver().setup();
            ChromeOptions options = new ChromeOptions();
            Map<String, Object> preferences = new HashMap<String, Object>();
            preferences.put("download.prompt_for_download", false);
            preferences.put("plugins.always_open_pdf_externally", true);
            preferences.put("download.open_pdf_in_system_reader", false);
            preferences.put("profile.default_content_settings.popups", 0);

            preferences.put("download.default_directory", "/Users/username/Downloads/");

      //Replace above value with the download directory of your system

            options.setExperimentalOption("prefs", preferences);
            WebDriver driver = new ChromeDriver(options);
            driver.manage().timeouts().implicitlyWait(30, 
            TimeUnit.SECONDS);
            driver.get("https://www.princexml.com/samples/");
            driver.findElement(By.xpath("//a[@href=\"https://css4.pub/2015/icelandic/dictionary.pdf\"]")).click();
Abhay Chaudhary
  • 1,763
  • 1
  • 8
  • 13