Buras Buras - 3 months ago 9
Java Question

How to save .pdf file that pops up in a browser window with no url?

I am working with

.pdf
files that are available on my companies' website only. I am not aware of any way to download them and store in one folder.

The link that I click to get the
.pdf
file has the following source code:

<a href="javascript:propertiesView('documentName')">


As I click on the link, a
.pdf
file pops up in a new browser window with no url and no source code. I presume that there is no way to manipulate that
.pdf
directly, then how can I save it then in order to manipulate the
.pdfs
from a folder
?

Thank You

Alp Alp
Answer

You may have luck by simply telling your browser to always save PDF files to disk (credits to Dirk):

firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

If that doesn't work, you are probably able to iterate through all open windows/tabs by using the switchTo() methods. Try something like this to get some insight about your opened windows (credits to Prashant Shukla):

    public void getWindows() {
        Set<String> windows = driver.getWindowHandles();

        for (String window : windows) {
            driver.switchTo().window(window);
            System.out.println(driver.getTitle());
        }
    }

A non-selenium solution to download the file would be to use the apache-commons library (creadits to Shengyuan Lu):

org.apache.commons.io.FileUtils.copyURLToFile(URL, File)

But this would require that you know the URL of the window, which you probably are able to fetch with the second approach i mentioned (driver.switchTo()) and driver.getCurrentUrl().

Comments