webbrowser控件c#仅在下次不工作时第一次调用
我试图从网站提取一些数据,但submitbutton仅在document completed事件中第一次调用。加载第一个提交的页面后,document completed事件未执行 我的代码是webbrowser控件c#仅在下次不工作时第一次调用,c#,visual-studio-2008,C#,Visual Studio 2008,我试图从网站提取一些数据,但submitbutton仅在document completed事件中第一次调用。加载第一个提交的页面后,document completed事件未执行 我的代码是 private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser b = sender as WebBrowser; strin
private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = sender as WebBrowser;
string response = "";
response = b.DocumentText;
HtmlElement links = b.Document.GetElementById("btn1");
links.InvokeMember("click");
checkTrafiicButtonClick = true;
MessageBox.Show("");
***// upto these working and loading second page after that i want to fill data
and want to submit but down line is not working and it should be work after
loading the page that i submitted how can i do these***
HtmlElement tfrno = b.Document.GetElementById("TrfNo");
tfrno.SetAttribute("value", "50012079");
HtmlElement submitButon = b.Document.GetElementById("submitBttn");
submitButon.InvokeMember("click");
}
根据您的评论,您的代码将无法按预期工作。第一次单击后,WebBrowser开始异步页面加载。您应该执行以下操作:
private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = sender as WebBrowser;
if(b.Url.AbsoluteUri == "mydomain.com/page1.html")
{
string response = "";
response = b.DocumentText;
HtmlElement links = b.Document.GetElementById("btn1");
links.InvokeMember("click");
checkTrafiicButtonClick = true;
MessageBox.Show("");
return;
}
***// upto these working and loading second page after that i want to fill data
and want to submit but down line is not working and it should be work after
loading the page that i submitted how can i do these***
if(b.Url.AbsoluteUri == "mydomain.com//page2.htm")
{
HtmlElement tfrno = b.Document.GetElementById("TrfNo");
tfrno.SetAttribute("value", "50012079");
HtmlElement submitButon = b.Document.GetElementById("submitBttn");
submitButon.InvokeMember("click");
return;
}
}
另外请注意,页面的某些部分可以从不同的源加载,例如在
iframe
中,因此您必须根据您的评论检查适当的uri
,您的代码将无法按预期工作。第一次单击后,WebBrowser开始异步页面加载。您应该执行以下操作:
private void b_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = sender as WebBrowser;
if(b.Url.AbsoluteUri == "mydomain.com/page1.html")
{
string response = "";
response = b.DocumentText;
HtmlElement links = b.Document.GetElementById("btn1");
links.InvokeMember("click");
checkTrafiicButtonClick = true;
MessageBox.Show("");
return;
}
***// upto these working and loading second page after that i want to fill data
and want to submit but down line is not working and it should be work after
loading the page that i submitted how can i do these***
if(b.Url.AbsoluteUri == "mydomain.com//page2.htm")
{
HtmlElement tfrno = b.Document.GetElementById("TrfNo");
tfrno.SetAttribute("value", "50012079");
HtmlElement submitButon = b.Document.GetElementById("submitBttn");
submitButon.InvokeMember("click");
return;
}
}
还要注意,页面的某些部分可以从不同的源加载,例如在
iframe
中,因此,您必须检查正确的uri
btn1是否单击加载新页面?是的,第一页btn1单击执行和第二页加载,但我想填写第二页的输入框并提交,只需添加一个跟踪您所在页面的变量。btn1是否单击加载新页面?是,第一页btn1单击执行和加载第二页加载但我想填写第二页的输入框并提交只需添加一个变量跟踪您所在的页面。是的,实际上第二页某些数据是从iframe中提交的另一个url如何检查框架加载是否完成?@eldhose DocumentCompleted将为每个加载的url触发。使用fiddler计算出框架的地址,然后检查是否已加载。是的,实际上第二页某些数据是从iframe其他url中提交的。如何检查框架加载是否完成?@eldhose DocumentCompleted将为每个加载的url激发。使用fiddler计算出框架的地址,然后检查是否已加载。