简体   繁体   English

如何在for循环中使用Web浏览器控件?

[英]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. 我能够提交表单并选择元素,但是问题是我想循环使用Web浏览器控件。

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 . 基本上我想导航到url并提交表单,并从返回的HTml选择一些元素。 So i used webBrowser1_DocumentCompleted . 所以我用了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. 我想完成一个文档然后移至其他文档,但问题是首先循环完成,然后最后只调用一次webBrowser1_DocumentCompleted
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: 每次完成文档加载后,您都可以导航到下一个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. 背景是,WebBrowser只能同时导航到一个网站。 It is like you enter a web adress in the adress bar and hit the enter key. 就像您在地址栏中输入网络地址并按Enter键一样。 Then you enter a second adress before the first site was loaded completly. 然后,在第一个站点完全加载之前,输入第二个地址。 The first loading process will be cancelled. 第一次加载过程将被取消。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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