0
votes

I am writing a web-scraping program using WatiN on a Windows 7 Machine running Internet Explorer 11. My PDF Viewer is Foxit Reader, and it is currently working inside Internet Explorer.

My goal: I am trying to find a way to click on a link which loads a pop-up window that displays a .pdf file; however, the normal methods used in Watin to identify the pop-up window don't seem to be working. In other words, Watin can't find the pop-up window ... and I'm at a loss at what to do.

To begin, I am clicking on the following link within the target web page:

<a onclick="window.open('http://go.website.com/DocumentLoader.aspx?file=3%2f111978%2fatt%2f142245916_OPT.pdf','attachment','width=960px,height=640px,resizable=no')" href="javascript:void(0)">0017384611</a>

I am able to click on this link using the following piece of WatiN code:

browser.Link(lnk => lnk.GetAttributeValue("onclick") != null && lnk.GetAttributeValue("onclick").Contains("_OPT.pdf")).ClickNoWait();

This part works and results in a new Internet Explorer window that opens up with a .pdf file displayed inside.

I then attempt to create a new browser object for that window using the following code snippet:

IE popup = IE.AttachTo<IE>(Find.ByUrl(url => url != null && url.Contains("DocumentLoader.aspx")));

However, this fails to identify the window, although the new pop-up window has a URL that includes the filename "DocumentLoader.aspx" (see the original link for this).

What's weird is that when I've queried the InternetExplorer object to get all of its windows, the one with the .pdf file doesn't appear at all. I don't know why this is, although I suspect it has to do with the fact that the window is displaying a .pdf file and not a normal HTML file.

I've also tried opening up a new browser object with the DocumentLoader.aspx URL but WatiN doesn't seem to be able to do this (it keeps timing out).

Can anyone suggest how I can get WatiN to recognize the new pop-up window?

Thanks!

UPDATE: After the pop-up window appears, I tested to see if Watin detected the new window using the following code:

var instances = new IECollection(true);
Console.WriteLine("Instances = " + instances.Count);

It's showing a count of 1 although, as the image below demonstrates, there are two Internet Explorer windows open:

enter image description here

1

1 Answers

1
votes

I can offer a different approach. I was doing the same thing that you were doing but in the end I was caught up too much in WatIn and I could't see that the solution was much simpler than anything else. If you do have the link to the file you can just download the PDF and bypass the preview altogether (if there's no user interaction this step is useless and if there is a user looking at the screen you can just do what you have been doing, display the PDF and also do what's listed down here:

using (WebClient client = new WebClient())
{
   if (Directory.Exists(@"\\folder"))
   {
       string downloadURL = "http://example.com/retrievePDF.jsp?id=XXXXX";
       client.DownloadFile(downloadURL, @"\\folder\" + fName + ".pdf");
   }
}

in your particular case you will be getting the dwonloadURL from the href link (which you already have)

Hope it helps you or somebody coming after you posted this.