Attaching Browser Variable To PDF File In Internet Explorer

时间:2015-07-28 16:39:48

标签: c# internet-explorer watin

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 个答案:

答案 0 :(得分:1)

我可以提供不同的方法。我正在做你正在做的事情,但最后我在WatIn中被抓得太多了,我看不出解决方案比其他任何东西都简单得多。如果你有文件的链接,你可以下载PDF并完全绕过预览(如果没有用户交互,这一步是没用的,如果有用户看着屏幕,你可以做你正在做的事情,显示PDF并执行下面列出的内容:

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

在您的特定情况下,您将从href链接(您已经拥有)获取dwonloadURL

希望它可以帮助您或者在您发布此内容之后找人。