Downloading pdf with Selenium WebDriver for Firefox

Viewed 4344

I am trying to download a .pdf to my local so that I can use Apache PDFBox to read the text from it and verify it as a part of my test suite. I have already found some code to download the pdf from Firefox by hitting a URL. This does not work for me since the pdf I am working with is a confidential document so it is not exposed by a URL, instead loaded within PDF Viewer as a popup window. Does anyone know how to hit the download button within the Firefox PDF Viewer after I have loaded the PDF Viewer in the browser?

enter image description here

I have tried looking it up by the element's id which = "download":

(new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("download")));
driver.findElement(By.id("download")).click(); 

Unfortunately this does not work as it says it cannot find the element. Anyone know a workaround?

UPDATE: The pop-up window I described was an iframe element. This caused the inability to find the "download" element. Fixed with @4M01's switchTo() answer.

4 Answers

As you mentioned,

instead loaded within PDF Viewer as a popup window

You need to handle switching between different windows using switchTo() method of the driver object.

Below code working fine for me without an issue and I'm able to click on download icon.

public class FirefoxPDFTest {
      WebDriver driver;

    @BeforeClass
    void Setup(){
        System.setProperty("webdriver.gecko.driver", "C:\\Automation\\Selenium\\drivers\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }

    @Test
    void downloadPDF(){
        driver.get("http://www.pdf995.com/samples/pdf.pdf");
        waitTillPageLoad();
        driver.findElement(By.id("download")).click();
    }



    private void waitTillPageLoad(){
        new WebDriverWait(driver, 30).until(driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete"));
    }


    @AfterClass
    void tearDown(){
        driver.close();
        driver.quit();
    }

}

just use following code for clicking download button:

    driver.findElement(By.xpath("//button[@id='download']")).click();

    Thread.sleep(8000);

    Robot robot = new Robot();

    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

We can handle the download popup in Firefox browser using Firefox browser settings and Firefox Profile setting using WebDriver.

Step 1: Update the setting in Firefox browser.

Open Firefox browser and navigate to Tools -> Options Navigate to Applications. Set the Action type as ‘Save File’ for PDF.

Step 2: Initialize FireFoxDriver using FirefoxProfile

File downloadsDir = new File("");

// Set Preferences for FirefoxProfile.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", downloadsDir.getAbsolutePath());
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
      "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);

// Initialize the FireFoxDriver instance.
FirefoxDriver webDriver = new FirefoxDriver(profile);

Step 3: Execute the Script

Execute the script which clicks on the download PDF icon.

Result: PDF file will be downloaded and the Download popup will not be displayed.

Related