找回密码
 加入
搜索
查看: 2501|回复: 0

[IE类操作] 怎样转换为au3

[复制链接]
发表于 2010-9-18 02:58:54 | 显示全部楼层 |阅读模式
一、屏蔽alert、confirm、showModalDialog源代码:

例1、先引用COM组建mshtml;




引用名称空间mshtml:

using mshtml;

然后处理WebBrowser控件的Navigated事件,代码如下:

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)   
        {   
            IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;   
            string s = "window.alert = null;\r\nwindow.confirm = null;\r\nwindow.open = null;\r\nwindow.showModalDialog = null;";   
            win.execScript(s, "javascript");   
}   
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
            string s = "window.alert = null;\r\nwindow.confirm = null;\r\nwindow.open = null;\r\nwindow.showModalDialog = null;";
            win.execScript(s, "javascript");
}

例2、

我们在使用WebBrowser得到网页代码模拟操作的时候,常常会因为目标网页有弹出对话框(alert、confirm)而中断我们操作,网上也有关于屏蔽这些对话框的介绍,比如另开一线程来定时确定,或是采用FindWindow等方法来进行关闭对话框,但这些办法给人感觉总有点烦琐;其实我们只要在目标网页中重载alert和confirm函数,让它们一直都返回true,那就ok拉,下面我们来说说在.net2.0中如何实现:

该办法要用到IHTMLWindow2类,所以我们要先添加一个引用:在项目引用里面选择COM选项卡,添加Microsoft HTML Object Library,然后在我们的cs文件中引如名称空间:mshtml;

然后取得我们目标页面的HtmlDocument,比如:
HtmlDocument hd = webBrowser1.Document.Window.Frames[0].Document;

接着进行最重要的一步,重载alert,confirm函数:
IHTMLWindow2 win = (IHTMLWindow2)hd.Window.DomWindow;
string s = @"function confirm() {";
s += @"return true;";
s += @"}";
s += @"function alert() {}";
win.execScript(s, "javascript");

OK ,大功告成,我们可以继续我们正常的操作拉,比如填写表单并提交:

hd.All["username"].SetAttribute("value","username");
hd.All["password"].SetAttribute("value","password");
hd.All["buttom"].InvokeMember("click");

运行一下,烦人的alert和confirm总算不见了



二、关闭调试对话框:

处理webbrowser的NewWindow事件,设定其传入的参数Cancel=false即可。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-18 17:33 , Processed in 0.078392 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表