如何在 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.
for (int i = 3910001; i < 391537; i++)
{
webBrowser1.Navigate(url);
}
基本上我想导航到 url 并提交表单并从返回的 HTml
中选择一些元素.所以我使用了 webBrowser1_DocumentCompleted
.
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");
}
我想完成一个文档然后移动到另一个但问题是首先循环完成然后最后 webBrowser1_DocumentCompleted
只被调用一次.
这个问题有解决方案吗?
谢谢
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);
}
背景是,WebBrowser 只能同时导航到一个网站.就像您在地址栏中输入一个网址并按回车键一样.然后在第一个站点完全加载之前输入第二个地址.第一次加载过程将被取消.
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.
这篇关于如何在 for 循环中使用 Web 浏览器控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!