none
webbrowser如何点击弹出的提示框呢? RRS feed

  • 问题

  • 开发环境:vs2008  ,Windows7系统

    我想实现的功能是能够用代码自动点击弹出提示框的 确定按钮

    以下是代码,只有一个简单的winform,winform窗体有两个按钮和一个webbrowser控件

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace LonginEmailForm08
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                button1.Visible = false;
                webBrowser1.Navigate(new Uri("http://mail.teksundigital.com/extmail/cgi/index.cgi"));
                //webBrowser1.Navigate(new Uri("http://mail.126.com"));
            }
            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                if (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                    return;
                //Thread.Sleep(6000); //按网速调整时间
                HtmlElement tbuserName = this.webBrowser1.Document.All["username"]; //获取登录对应元素  
                HtmlElement tbpassword = this.webBrowser1.Document.All["password"];
                HtmlElement tbsubmit = this.webBrowser1.Document.Forms["login"];
                if (tbuserName != null)
                    tbuserName.SetAttribute("value", "");  //给用户名赋值  
                if (tbpassword != null)
                    tbpassword.SetAttribute("value", "");    //给密码赋值
                if (tbsubmit != null)
                    tbsubmit.InvokeMember("submit"); //模拟点击登录按钮  
                //else
                //    this.webBrowser1.Refresh(); 
                //webBrowser1.Navigate(new Uri(this.webBrowser1.Document.Url.ToString()));
                HtmlElement navMail = this.webBrowser1.Document.GetElementById("nav_mail");
                if (navMail != null)
                    navMail.InvokeMember("click"); //模拟点击登录按钮 
    
    
                //MessageBox.Show(webBrowser1.Document.Window.Frames["main"].Document.Body.InnerHtml);
                if (this.webBrowser1.Document.Window.Frames.Count != 0)
                {
                    navMail = this.webBrowser1.Document.Window.Frames["main"].Document.GetElementById("nav_mail");
                    if (navMail != null)
                    {
                        if (this.webBrowser1.Document.Window.Frames["main"].Document.Title.ToString() != "ExtMail - 收件箱 - wuzhen@teksundigital.com")
                        {
                            herfclick("nav_mail");
                        }
                        //navMail.InvokeMember("click");
    
                    }
                }
    
            }
    
            /// <summary>
            /// 模拟点击收件箱超链接
            /// </summary>
            /// <param name="url"></param>
            private void herfclick(string id)
            {
                if (this.webBrowser1.Document.Window.Frames.Count == 0)
                    return;
                for (int i = 0; i < this.webBrowser1.Document.Window.Frames["main"].Document.All.Count; i++)
                {
                    if (this.webBrowser1.Document.Window.Frames["main"].Document.All[i].TagName == "A" && this.webBrowser1.Document.Window.Frames["main"].Document.All[i-1].GetAttribute("id").ToString().Trim() == id)
                    {
                        this.webBrowser1.Document.Window.Frames["main"].Document.All[i].InvokeMember("click");//引发”CLICK”事件
                        break;
                    }
                }
    
            }
    
            /// <summary>
            /// 模拟点击全选所有邮件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btn_SelectAllEmail_Click(object sender, EventArgs e)
            {
    
                
                HtmlElement chName = this.webBrowser1.Document.Window.Frames["main"].Document.All["SELECTALL"];
                if (chName != null)
                    chName.InvokeMember("click"); //模拟点击全选按钮 
                //点击删除按钮
                HtmlElement btndelete = this.webBrowser1.Document.Window.Frames["main"].Document.All["btn_delete"];
                if (btndelete != null)
                    btndelete.InvokeMember("click"); 
            
            }
    
    
    
        }
    }


    给我写信: QQ我:点击这里给我发消息



    2013年5月18日 11:52

答案

  • 需要在控件站点上实现IDocHostShowUI


    Visual C++ MVP

    2013年5月18日 21:11
    版主
  • 你可以试试下面的代码

    webBrowser1.Document.InvokeScript("function  confirm(){return true;}");


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. my sample

     这样的代码你试试的

    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. my sample

    2013年5月25日 2:58

全部回复