dfgv
dfgv

Reputation: 97

webbrowser control c# only invoke first time next time not working

I am trying to extract some data from the website but the submitbutton invoke only first time in document completed event.after loading the first submitted page document completed event not executing my code is

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

    }

Upvotes: 1

Views: 880

Answers (1)

VladL
VladL

Reputation: 13043

Based on your comment, your code will not work as expected. After the first click the webbroser starts asynchronous page loading. You should do following:

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;
     }
    }

Also note, that some parts of the page can be loaded from different source, for example in iframe, so you have to check proper uri

Upvotes: 1

Related Questions