简体   繁体   中英

How to use Web Browser Control in for loop?

I am working on web crawler in which it checks the results of students from same website. I am able to submit the form and select elements but the problem is that i want to use web browser control in a loop.

for (int i = 3910001; i < 391537; i++)
    {
      webBrowser1.Navigate(url);      
    }

Basically i want to navigate to url and submit the form and pick some elements from the returned HTml . So i used webBrowser1_DocumentCompleted .

private void webBrowser1_DocumentCompleted(object sender, 

    WebBrowserDocumentCompletedEventArgs e)
        {
       // MessageBox.Show("I am in completed");

          HtmlElement form = webBrowser1.Document.GetElementById("form1");
          webBrowser1.Document.GetElementById("RollNo").SetAttribute("value", "100");
          HtmlElement btn = webBrowser1.Document.GetElementById("Submit");
          btn.InvokeMember("click");

        }

I want to finish one document then move to other but the problem is that first the loop completes then in the end webBrowser1_DocumentCompleted is called only once.
Is there a solution to this problem?
Thanks

You need to remove your loop and replace it with a little bit more complex logic.
Every time a document was complete loaded you can navigate to the next url:

int count = 3910001;//that's your number
private void webBrowser1_DocumentCompleted(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    HtmlElement form = webBrowser1.Document.GetElementById("form1");
    webBrowser1.Document.GetElementById("RollNo").SetAttribute("value", "100");
    HtmlElement btn = webBrowser1.Document.GetElementById("Submit");
    btn.InvokeMember("click");

    ++count;
    if(count<391537)//that's your Number too, but it does not make sense, Count is always smaller than 391537
        webBrowser1.Navigate(url);
}

The background is, that a WebBrowser can only navigate to one website at the same time. It is like you enter a web adress in the adress bar and hit the enter key. Then you enter a second adress before the first site was loaded completly. The first loading process will be cancelled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM