1

我正在尝试制作一个将网页加载到表单中的小程序,自动在页面上提交表单 - 这是一个更大的项目的一部分,但我无法让这部分正常工作。

当输入并提交正确的案件编号和日期 (mm-yy) 时,此网页会提供法院案件结果。

我在表单中创建了一个简单的 webBrowser 并将其命名为webBrowser1.

这是我的 Form.cs 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(HandleRequest);
            firstStep();
           // secondStep();
        }

        private void firstStep()
        {
            webBrowser1.Url = new System.Uri("http://www.court.gov.il/NGCS.Web.Site/HomePage.aspx", System.UriKind.Absolute);
        }

        private void HandleRequest(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
                webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(HandleRequest);
                HtmlElement caseNumber = ((WebBrowser)sender).Document.All["Header1_CaseLocatorHeaderUC2_BamaCaseNumberTextBoxHT"];
                HtmlElement caseDate = ((WebBrowser)sender).Document.All["Header1_CaseLocatorHeaderUC2_BamaMonthYearTextBoxHT"];

                caseNumber.Focus();
                System.Windows.Forms.SendKeys.Send("(1)");
                System.Windows.Forms.SendKeys.Send("(2)");
                System.Windows.Forms.SendKeys.Send("(2)");
                System.Windows.Forms.SendKeys.Send("(3)");
                System.Windows.Forms.SendKeys.Send("(8)");
                System.Windows.Forms.SendKeys.Send("{TAB}");
                System.Windows.Forms.SendKeys.Send("(0)");
                System.Windows.Forms.SendKeys.Send("(3)");
                System.Windows.Forms.SendKeys.Send("(1)");
                System.Windows.Forms.SendKeys.Send("(0)");

                HtmlElement inputTag = webBrowser1.Document.All["Header1_CaseLocatorHeaderUC2_SearchHeaderCaseButton"];
                inputTag.InvokeMember("Click");

        }
    }
}

例子

如您所见 - 我正在输入如图所示的值,然后单击提交此表单的按钮,但它不起作用!?这个网页非常棘手,它使用验证输入的脚本,然后设置要提交的正确值,顺便说一下,他只在 IE < 10....

有什么想法吗?

编辑: 它现在正在工作 - 我正在解雇invoke两次,但我不知道问题出在哪里。

现在的新问题是我添加了一个按钮单击来触发该过程btnGet_Click,并且在触发此功能时出现错误-似乎DetailsTag已设置为空...但是当我在public Form1()其正常工作和加载中取消注释该功能时网站应有的样子。

firstStep()在按钮单击/公共表单中调用函数有什么区别?

为什么我会收到此错误?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private readonly object lock_ = new object();
        HtmlElement caseNumber;
        HtmlElement caseDate;
        HtmlElement DetailsTag; 
        int checks = 1;

        public Form1()
        {
            InitializeComponent();
            //webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(HandleRequest);
            //firstStep();
        }

        private void firstStep()
        {


                webBrowser1.Url = new System.Uri("http://www.court.gov.il/NGCS.Web.Site/HomePage.aspx", System.UriKind.Absolute);

        }       
        private void HandleRequest(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
                if (checks < 1)
                {
                    webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(HandleRequest);
                    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetDetails);
                }
                caseNumber = ((WebBrowser)sender).Document.All["Header1_CaseLocatorHeaderUC2_BamaCaseNumberTextBoxHT"];
                caseDate = ((WebBrowser)sender).Document.All["Header1_CaseLocatorHeaderUC2_BamaMonthYearTextBoxHT"];

                if (caseNumber != null && caseDate != null)
                {

                caseNumber.Focus();
                System.Windows.Forms.SendKeys.Send("(5)");
                System.Windows.Forms.SendKeys.Send("(6)");
                System.Windows.Forms.SendKeys.Send("(5)");
                System.Windows.Forms.SendKeys.Send("(8)");
                System.Windows.Forms.SendKeys.Send("{TAB}");
                System.Windows.Forms.SendKeys.Send("(0)");
                System.Windows.Forms.SendKeys.Send("(8)");
                System.Windows.Forms.SendKeys.Send("(1)");
                System.Windows.Forms.SendKeys.Send("(3)");

                checks = 0;

                System.Windows.Forms.SendKeys.Send("{ENTER}");

                }
                else
                {
                    MessageBox.Show("No such case - enter a new one");
                }
        }
        private void GetDetails(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
                webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(GetDetails);
                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetResults);

                DetailsTag = webBrowser1.Document.All["_ctl0_caseDetailsGrid_row1_ct6_Imagebutton1"];
                if (DetailsTag != null)
                {
                    DetailsTag.InvokeMember("Click");
                }
                else
                {
                    MessageBox.Show("Error - try another case!");
                }
        }
        private void GetResults(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
                webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(GetResults);
                HtmlElement DetailsStatus = webBrowser1.Document.All["_ctl7_caseStatusIDlbl"];
                HtmlElement DetailsCourt = webBrowser1.Document.All["_ctl7_courtIDlbl"];
                HtmlElement DetailsType = webBrowser1.Document.All["_ctl7_caseTypeIDlbl"];
                HtmlElement DetailsAmount = webBrowser1.Document.All["_ctl7_claimAmountlbl"];
                HtmlElement DetailsPrev = webBrowser1.Document.All["_ctl7_privilegeIDlbl"];

                txtAmount.Text = DetailsAmount.InnerText;
                txtCount.Text = DetailsCourt.InnerText;
                txtPrev.Text = DetailsPrev.InnerText;
                txtStatus.Text = DetailsStatus.InnerText;
                txtType.Text = DetailsType.InnerText;
        }

        public void btnGet_Click(object sender, EventArgs e)
        {
                webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(HandleRequest);
                firstStep();
        }
    }
}
4

1 回答 1

1

正如评论所说,您填充文本框的方法不是太正统,实际上,在我的电脑上不起作用。通常的程序是依赖SetAttribute函数。您的代码将变为:

caseNumber.SetAttribute("value", "12283");
caseDate.SetAttribute("value", "03-10"); 

如果我这样做然后我使用您的inputTag.InvokeMember("Click"),则提交表单(至少,不会出现弹出窗口并且浏览器被重定向到新页面)。

于 2013-08-15T18:51:46.150 回答