参照网上资料,使用webbrowser控件做了一个自动登录网站系统的功能 ,可以登录进某网站。但是点击其中一个新开窗口的页面(就是标记为target=_blank),本来是自动到显示明细数据的页面,结果弹出的是初始登录的页面;其他在form窗体内部的链接可以点进显示明细。
会不会webbrowser 没有保存session,新开窗口(页面)的时候返回开始登录页面。
主要代码:
 private void Form1_Load(object sender, EventArgs e)
        {
            //WebBrowser webBrowser1=new WebBrowser();
            webBrowser1.Navigate(url);        }  private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {   //WebBrowser webBrowser1=new WebBrowser();
            //webBrowser完成页面加载:
        if (webBrowser1.Url.ToString() == "url") 
        {
            HtmlDocument doc = webBrowser1.Document; //获取document对象
            HtmlElement btn = null;
            foreach (HtmlElement em in doc.All) //轮循
            {
                string str = em.Name;
                if ((str == "txtConsultantID") || (str == "txtPassword") || (str == "imgLogin1")) //减少处理
                {
                    switch (str)
                    {
                        case "txtConsultantID": em.SetAttribute("value", "用户名"); break; //赋用户名
                        case "txtPassword": em.SetAttribute("value", "密码"); break; //赋密码
                        case "imgLogin1": btn = em; break; //获取submit按钮
                        default: break;
                    }                }
            }            btn.InvokeMember("click"); //触发submit事件
            //doc.Forms["Form1"].InvokeMember("submit");
                  }  
                  }

解决方案 »

  1.   

    新开的窗口是另一个进程吧
    Session是Inproc的
    你可以阻止弹出,在本webbrower里面打开新页面
      

  2.   

    不幸的是.net提供的WebBrowser没有提供NewWindow3事件。因此你需要使用
    AxSHDocVw.AxWebBrowser,并订阅他的NewWindow3事件,在事件处理里面写
    e.cancel = true;
    _browser.Navigate(e.bstrUrl)至于如何使用AxSHDocVw.AxWebBrowser,请参考
    Start Visual Studio .NET.
    Create a new Visual C# .NET Windows Application project.
    Right-click in the Toolbox window, and then click Customize Toolbox.
    On the COM Components tab, select the Microsoft Web Browser check box. Notice that the WebBrowser control is added to the Controls tab in the toolbox that you chose to customize from.
    Drag the WebBrowser control from the toolbox to the Visual C# .NET form. AxWebBrowser1 is the default name.
      

  3.   

    谢谢 风吹过给思路。该问题我解决了。webbrower 同样可以解决。加入newwindow事件就可以,可以在一个新的并且和form一样的窗体打开页面。
     private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
            {
                String newUrl = "your url";
                Form1 newform = new Form1();
                newform.Show();
                newform.webBrowser1.Navigate(newUrl);
                e.Cancel = true;
               
            }
      

  4.   

    我做了个自动登录淘宝网的东西,Webbrowse自动登录进淘宝后,手动点击页面的连接,不会登出来.但是我想让它自己点"领取当日淘金币"这个连接,应该怎么弄呢?
    2 登录成功后,我给webbrowse传了个淘宝首页的地址,可以转到首页,但是帐号自动登出来了.这个问题怎么解决呢.
     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                //获取从数据库中找到的用户名、密码并填入网页中
                string u = comboBox1.Text;
                string k = textBox2.Text;
                webBrowser1.Document.GetElementById("J_SafeLoginCheck").InvokeMember("click");
                webBrowser1.Document.GetElementById("TPL_username_1").InnerText = u;
                webBrowser1.Document.All.GetElementsByName("TPL_password")[0].InnerText = k;
                HtmlElement formLogin = webBrowser1.Document.Forms["J_StaticForm"];
                formLogin.InvokeMember("submit"); //点击登录按钮            //查找"领取当日淘金币" 并点击,转到领淘金币页面               e.cancel = true;
                   webBrowser1.Navigate(e.bstrUrl);            webBrowser1.DocumentText.Equals("领取当日淘金币");            Thread.Sleep(5000);
                herfclick("http://www.taobao.com/"); //这是登录成功的操作,跳转到taobao首页
                //注意不是直接跳过去的,模拟点击链接. 这里我想自动跳到首页,应该怎么弄呢?
                //SESSION不会丢             //模拟点击链接:
                        自动点击"淘宝网首页"使页面跳转到首页
                
                for (int i=1; i<webBrowser1.Document.All.Count; i++)
                {
                    
                }
            }               private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
            {
                //防止弹窗,防止在IE中打开网页;
                e.Cancel = true;
                string url = this.webBrowser1.StatusText;
                this.webBrowser1.Url = new Uri(url);        }