当前位置 : 主页 > 网络编程 > net编程 >

webBrowser捕获Alert内容(C#)

来源:互联网 收集:自由互联 发布时间:2023-10-08
1.引用using mshtml; //Microsoft HTML Object Library 2.[ComVisible(true)] //放到类的上边 public partial class Form1 : Form 3.//激活webBrowser1_Navigated事件 private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEve

1.引用using mshtml;  //Microsoft HTML Object Library

2.[ComVisible(true)] //放到类的上边

public partial class Form1 : Form

3.//激活webBrowser1_Navigated事件

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
IHTMLWindow2 ihtmlWin = webBrowser1.Document.Window.DomWindow as IHTMLWindow2;
StringBuilder strAlertBuilder = new StringBuilder();
strAlertBuilder.Append("var _alert=window.alert;\n");
strAlertBuilder.Append("window.alert=function(s){\n");
strAlertBuilder.Append("window.external.alertMessage(s);}");
ihtmlWin.execScript(strAlertBuilder.ToString(), "Javascript");
}

4. //接收alert的内容

public void alertMessage(string s)
{
this.textBox1.Text = s;
}

5.关键

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ObjectForScripting = this;//此属性是得到alert关键设置,否则可以禁止弹出,但是无法得到信息
}


上一篇:webbrowser 常用方法(C#)
下一篇:没有了
网友评论