Buras
Buras

Reputation: 3099

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

Upvotes: 3

Views: 7075

Answers (3)

user521990
user521990

Reputation: 829

If you want to save a PDF to your hard drive in IE with selenium, you need to use pywinauto with selenium. I just used this code for PDF files that open up in the browser.

//selenium imports
from pywinauto import application //pywinauto import

//write selenium code to open up pdf in the browser 
driver = webdriver.Ie("IEDriverServer.exe", capabilities = caps)

//this could be a get or driver.execute_script() to click a link
driver.get("link to pdf")

//save pdf
app = application.Application()

//get the ie window by the title of the application (assuming only one window is open here)
ie =  app.window_(title_re = ".*Internet Explorer.*")

//this line focuses on the pdf that is open in the browser
static = ie.Static

//focus on the pdf so we can access the internal controls
static.SetFocus()

//control + h shows the pdf bar, but you don't really need this step 
//for it to work. i just used it as a debug
static.TypeKeys("^H")

//open save file dialog
static.TypeKeys("+^S")

//tricky here because the save file dialog opens up as another app instance   
//which is how pywinauto sees it
app2 = application.Application()

//bind to the window by title - name of the dialog
save = app2.window_(title_re = ".*Save As.*")

//this is the name of the property where you type in the filename
//way to be undescriptive microsoft
file_name = save[u'FloatNotifySink']

//type in the file name
save.TypeKeys("hello")

//pause for a second - you don't have to do this
time.sleep(4)

//find and bind the save button
button = save[u'&SaveButton']

//click the save button
button.Click()

Upvotes: 2

Alp
Alp

Reputation: 29739

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().

Upvotes: 3

ddavison
ddavison

Reputation: 29032

I presume that there is no way to manipulate that .pdf directly

That's correct. With Selenium, you cannot.

how can I save it then in order to manipulate the .pdfs from a folder?

I've actually implemented this exact thing in our regression system where I work.

My first step, was to construct a Url based on what the propertiesView(). method did.

in your case, propertiesView() does some sort of window.open is my guess. So your goal is, to extract that Url that it opens, and use concatenation to construct the url.

Once you've found your Url, the rest is a cakewalk. Just download the url to a folder named /pdfs. See this question for how to do that.

It may even require calling that method to figure it out.. Due to my ignorance of your System Under Test, it's difficult for me to give you a code answer, unless you posted it.

A hint that i'll tell you, is if you are using Selenium 1, use

String url =selenium.getEval("var url = something; url;");

to fetch the url and get it into a java object. (If using selenium 2, use the JavaScriptExecutor#executeScript)

Upvotes: 2

Related Questions