Internet Explorer 自动化:如何抑制打开/保存对话框?
通过 MSHTML 控制 IE 实例时,如何抑制非 HTML 内容的打开/保存对话框?
我需要从另一个系统获取数据并将其导入到我们的系统中。 由于预算限制,另一方在一段时间内无法进行开发(例如 WS),所以我现在唯一的选择是进行网络抓取。
远程站点是基于 ASP.NET 的,因此简单的 HTML 请求将无法工作——太多的 JS。
我编写了一个简单的 C# 应用程序,它使用 MSHTML 和 SHDocView 来控制 IE 实例。 到目前为止一切顺利:我可以执行登录、导航到所需页面、填充必填字段并提交。
然后我面临几个问题:
首先是该报告在另一个窗口中打开。 我怀疑我也可以通过枚举系统中的 IE 窗口来附加到该窗口。
其次,更麻烦的是,报告本身是CSV文件,并触发打开/保存对话框。 我想避免它并让 IE 将文件保存到给定位置,或者我也可以通过编程方式单击对话框按钮(如何?)
我实际上完全不是 Windows 人员(unix/J2EE),并希望有人更好的知识会给我如何完成这些任务的提示。
谢谢!
更新
我在 MSDN 上找到了一份很有前途的文档: http://msdn.microsoft.com/en-ca/library/aa770041.aspx
控制下载的内容类型以及下载后 WebBrowser 控件对它们执行的操作。 例如,您可以阻止用户单击链接时播放视频、运行脚本或打开新窗口,或者阻止下载或执行 Microsoft ActiveX 控件。
慢慢阅读...
更新 2 :让它工作了,有点......
最后我让它工作了,但是以一种丑陋的方式。 本质上,我“在导航之前”注册一个处理程序,然后,在处理程序中,如果 URL 与我的目标文件匹配,我会取消导航,但记住 URL,并使用 WebClient 类直接访问和下载该临时 URL。
我无法在这里复制整个代码,它包含很多垃圾,但以下是基本部分:
安装处理程序:
_IE2.FileDownload += new DWebBrowserEvents2_FileDownloadEventHandler(IE2_FileDownload);
_IE.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(IE_OnBeforeNavigate2);
记录 URL,然后取消下载(从而防止出现“保存”对话框):
public string downloadUrl;
void IE_OnBeforeNavigate2(Object ob1, ref Object URL, ref Object Flags, ref Object Name, ref Object da, ref Object Head, ref bool Cancel)
{
Console.WriteLine("Before Navigate2 "+URL);
if (URL.ToString().EndsWith(".csv"))
{
Console.WriteLine("CSV file");
downloadUrl = URL.ToString();
}
Cancel = false;
}
void IE2_FileDownload(bool activeDocument, ref bool cancel)
{
Console.WriteLine("FileDownload, downloading "+downloadUrl+" instead");
cancel = true;
}
void IE_OnNewWindow2(ref Object o, ref bool cancel)
{
Console.WriteLine("OnNewWindow2");
_IE2 = new SHDocVw.InternetExplorer();
_IE2.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(IE_OnBeforeNavigate2);
_IE2.Visible = true;
o = _IE2;
_IE2.FileDownload += new DWebBrowserEvents2_FileDownloadEventHandler(IE2_FileDownload);
_IE2.Silent = true;
cancel = false;
return;
}
并在调用代码中使用找到的 URL直接下载:(
...
driver.ClickButton(".*_btnRunReport");
driver.WaitForComplete();
Thread.Sleep(10000);
WebClient Client = new WebClient();
Client.DownloadFile(driver.downloadUrl, "C:\\affinity.dump");
驱动程序是 IE 实例的简单包装 = _IE)
希望对某人有所帮助。
When controlling IE instance via MSHTML, how to suppress Open/Save dialogs for non-HTML content?
I need to get data from another system and import it into our one. Due to budget constraints no development (e.g. WS) can be done on the other side for some time, so my only option for now is to do web scraping.
The remote site is ASP.NET-based, so simple HTML requests won't work -- too much JS.
I wrote a simple C# application that uses MSHTML and SHDocView to control an IE instance. So far so good: I can perform login, navigate to desired page, populate required fields and do submit.
Then I face a couple of problems:
First is that report is opening in another window. I suspect I can attach to that window too by enumerating IE windows in the system.
Second, more troublesome, is that report itself is CSV file, and triggers Open/Save dialog. I'd like to avoid it and make IE save the file into given location OR I'm fine with programmatically clicking dialog buttons too (how?)
I'm actually totally non-Windows guy (unix/J2EE), and hope someone with better knowledge would give me a hint how to do those tasks.
Thanks!
UPDATE
I've found a promising document on MSDN: http://msdn.microsoft.com/en-ca/library/aa770041.aspx
Control the kinds of content that are downloaded and what the WebBrowser Control does with them once they are downloaded. For example, you can prevent videos from playing, script from running, or new windows from opening when users click on links, or prevent Microsoft ActiveX controls from downloading or executing.
Slowly reading through...
UPDATE 2: MADE IT WORK, SORT OF...
Finally I made it work, but in an ugly way. Essentially, I register a handler "before navigate", then, in the handler, if the URL is matching my target file, I cancel the navigation, but remember the URL, and use WebClient class to access and download that temporal URL directly.
I cannot copy the whole code here, it contains a lot of garbage, but here are the essential parts:
Installing handler:
_IE2.FileDownload += new DWebBrowserEvents2_FileDownloadEventHandler(IE2_FileDownload);
_IE.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(IE_OnBeforeNavigate2);
Recording URL and then cancelling download (thus preventing Save dialog to appear):
public string downloadUrl;
void IE_OnBeforeNavigate2(Object ob1, ref Object URL, ref Object Flags, ref Object Name, ref Object da, ref Object Head, ref bool Cancel)
{
Console.WriteLine("Before Navigate2 "+URL);
if (URL.ToString().EndsWith(".csv"))
{
Console.WriteLine("CSV file");
downloadUrl = URL.ToString();
}
Cancel = false;
}
void IE2_FileDownload(bool activeDocument, ref bool cancel)
{
Console.WriteLine("FileDownload, downloading "+downloadUrl+" instead");
cancel = true;
}
void IE_OnNewWindow2(ref Object o, ref bool cancel)
{
Console.WriteLine("OnNewWindow2");
_IE2 = new SHDocVw.InternetExplorer();
_IE2.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(IE_OnBeforeNavigate2);
_IE2.Visible = true;
o = _IE2;
_IE2.FileDownload += new DWebBrowserEvents2_FileDownloadEventHandler(IE2_FileDownload);
_IE2.Silent = true;
cancel = false;
return;
}
And in the calling code using the found URL for direct download:
...
driver.ClickButton(".*_btnRunReport");
driver.WaitForComplete();
Thread.Sleep(10000);
WebClient Client = new WebClient();
Client.DownloadFile(driver.downloadUrl, "C:\\affinity.dump");
(driver is a simple wrapper over IE instance = _IE)
Hope that helps someone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是在执行下载的系统上调整 CSV 文件的 MIME 类型。 由于与 .CSV 文件相关的操作,IE 正在尝试下载该文件。
我认为您可以通过转到“工具”-“文件夹选项”-“文件类型”在 Windows 资源管理器中更改此设置。 如果您将 CSV 文件与 Internet Explorer 关联,则 CSV 文件将在 IE 中打开。 此时您应该能够使用 IE 自动化将当前打开的文档保存到文件中。
The easiest way to do this would be to adjust the MIME type for CSV files on the system that does the downloading. IE is trying to download the file because of the action associated with .CSV files.
I think you can change this in Windows Explorer by going to Tools-Folder Options-File Types. If you associate CSV files with Internet Explorer then the CSV file will open in IE. At that point you should be able to use IE automation to save the current open document to a file.
您无法完全控制浏览器的行为方式,因为最终用户可以将其浏览器设置为始终打开具有特定内容类型的文件。
You cannot fully contol how the browser behaves, because the end user can set his browser to always open a file with a certain content type.