日期:2014-05-18  浏览次数:21014 次

WebBrowser 提交表单的问题
网页地址: www.cqjg.gov.cn/DriverFind.html

我要通软件实现查询功能,填写表单功能实现了,实现点击"查询"没有响应,不知道哪里有错误

代码如下:
C# code

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
                wb.Navigate("www.cqjg.gov.cn/DriverFind.html");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            #region 参数设置
            HtmlElement btnSubmit = wb.Document.All["formthree"]; //获取登录对应元素
            HtmlElement tbUserid = wb.Document.All["txtDname"];     //获取用户名对应元素
            HtmlElement tbStyle = wb.Document.All["txtCertificate"];     //获取证件号对应元素

            tbUserid.SetAttribute("value", "宋世");     //给用户名赋值
            tbStyle.SetAttribute("value", "1234567");    //给密码赋值
            btnSubmit.InvokeMember("submit"); //模拟点击登录按钮
            #endregion
        }



------解决方案--------------------
wb_DocumentCompleted 在这个事件中添加判断 


C# code

private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
            {
            #region 参数设置
            HtmlElement btnSubmit = wb.Document.All["formthree"]; //获取登录对应元素
            HtmlElement tbUserid = wb.Document.All["txtDname"];     //获取用户名对应元素
            HtmlElement tbStyle = wb.Document.All["txtCertificate"];     //获取证件号对应元素

            tbUserid.SetAttribute("value", "宋世");     //给用户名赋值
            tbStyle.SetAttribute("value", "1234567");    //给密码赋值
            btnSubmit.InvokeMember("submit"); //模拟点击登录按钮
            #endregion
           }
        }