2013-02-13 63 views
1

我想从网站中提取一些数据,但submitbutton只调用第一次在文档中完成event.after加载第一次提交的页面文档完成事件不执行 我的代码是网页浏览器控制c#只调用下次第一次不工作

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"); 

    } 
+0

不BTN1点击加载新一页? – VladL 2013-02-13 12:06:28

+0

是第一页btn1点击执行和第二页loadig,但我想填写第二页的输入框,并要提交 – dfgv 2013-02-13 12:10:50

+0

只需添加一个变量,以跟踪你的网页。 – 2013-02-13 13:20:23

回答

1

根据您的评论,您的代码将无法正常工作。第一次点击后,webbroser启动异步页面加载。你应该做以下几点:

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

+0

是的,实际上是第二页的一些数据从iframe的一些另一个url llr如何检查帧加载是否完成? – dfgv 2013-02-13 13:21:34

+0

@eldhose DocumentCompleted将针对每个加载的url启动。然后使用fiddler分配帧的地址并检查它是否已经被加载。 – VladL 2013-02-13 13:39:53