我正在尝试创建一个小程序,将一个网页加载到表单中,然后在表单上提交表单 页面自动 - 这是一个较大的项目的一部分,但我不能让这部分正常工作。
当输入并提交正确的案件编号和日期(mm-yy)时,会产生web page个案件。
我在表单中创建了一个简单的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
设置为null ...但是我取消对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();
}
}
}
答案 0 :(得分:1)
正如通过评论所说,你填充文本框的方法不太正统,实际上,我的计算机上没有。通常的程序依赖SetAttribute
函数。您的代码将成为:
caseNumber.SetAttribute("value", "12283");
caseDate.SetAttribute("value", "03-10");
如果我这样做,然后我使用你的inputTag.InvokeMember("Click")
,表单就会被提交(至少没有弹出窗口,浏览器会被重定向到新页面。)