比如用这控件加载个网页,登陆,然后弹个登陆成功,我怎么能靠代码获得登陆成功这4个字???

解决方案 »

  1.   

    webBrowser1.Navigate("javascript:function alert(str){ prompt('alert的值:', str); }");
      

  2.   

    有点不太明白你说的意思,是不是你想获取webbrowser装载的网页中用alert弹出消息的内容?
      

  3.   

    ①using System.Runtime.InteropServices;    添加mshtml引用using mshtml;
    ②添加一个webBrowser;在构造函数中添加webBrowser.ObjectForScripting = this;
    在类外写上[ComVisible(true)]
    ③写入webBrowser的DocumentCompleted事件,然后判断你用webBrowser转载的网页是不是弹消息框的网页如果是则执行
    IHTMLWindow2 ihtmlWin = htmlDoc.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);}");
    ④在你的类中加入一个public void alertMessage(string s)
    {MessageBox.Show(s);}
    试试那个s就是消息的内容;
      

  4.   

    8楼的方法不错,调试代码如下:
    [ComVisible(true)] 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }    private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentText = @"
                <html>
                <input type=""button"" value=""测试"" onclick=""alert('Zswang 路过');"">
                </html>
                ";
            while (webBrowser1.IsBusy) Application.DoEvents();
        }    public void alertMessage(string s)
        {
            MessageBox.Show(s, "囧");
        }    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Navigate(@"javascript:
                function alert(str)
                {
                    window.external.alertMessage(str);
                }");
            webBrowser1.ObjectForScripting = this;
        }
    }
      

  5.   

    忘了一步,在第三步的htmlDoc = webBrowser.Document;
    还要在第三步最后加上:ihtmlWin .execScript(strAlertBuilder.ToString(), "Javascript");
      

  6.   

    下面是我的代码,哪里有错吗???
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using mshtml;namespace WindowsApplication7
    {
        [ComVisible(true)]
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.webBrowser1.ObjectForScripting = this;
            }        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                IHTMLWindow2 ihtmlWin = this.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");
            }        private void alertMessage(string s)
            {
                this.textbox1.Text = s;
            }
        }
    }
      

  7.   

    private void alertMessage(string s) 
    改成
    public void alertMessage(string s)
      

  8.   

    下面那段代码到是成功了。可是,当我把webBrowser1的URL换成别的网站时,还是捕获不到弹出窗口的内容啊。
    我要访问的网页不是我自己写的。能捕获吗?using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace WindowsApplication7
    {
        [ComVisible(true)]
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                webBrowser1.Navigate(@"javascript:
                function alert(str)
                {
                    window.external.alertMessage(str);
                }");
                webBrowser1.ObjectForScripting = this;
            }        public void alertMessage(string s)
            {
                this.textBox1.Text = s;
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                webBrowser1.DocumentText = @"
                <html>
                <input type=""button"" value=""测试"" onclick=""alert('Zswang 路过');"">
                </html>
                ";
            }
        }
    }
      

  9.   

    我给你的代码没问题的,不知道你为什么要调用Form1_Load   ?
      

  10.   

    能告诉我你的QQ吗?我加你QQ说。