问题
I have a simple code that help me crawl to page 2 of Google result page:
var ie= new IE();
ie.Link(Find.ByText("2")).Click();
All I want is crawling to more next page with the inputed number of page, so that I make a loop like this:
string[] page = null;
for (int i = 0; i < NumOfPage; i++)
{
Array.Resize<string> (ref page, i+1);
page[i] = "\"" + i.ToString() + "\"";
}
int count=2;
while (count<NumOfPage)
{
ie.Link(Find.ByText(page[count])).Click();
count++;
}
But the result is it pause at the first page, no crawling to the next page. It's seem the loop doesn't work. Where is the problem???
回答1:
I think you should not use Click()
method to go to the next page, I recognized that Click()
will be performed only when the Link
is visible, so you have to scroll the vertical scrollbar to the bottom to show the Link
first (scrolling manually or programmatically works as you want). However I think to go to the next page, you can call the method GoTo()
instead with the Url
got from the found Link
. I've tested it OK but the delay between page switches is a little large (about 2 seconds or above). I don't know why you want to do this and would like to know it from you:
for (int i = 2; i < NumOfPage; i++)
{
ie.GoTo(ie.Link(WatiN.Core.Find.ByText(i.ToString())).Url);//Don't need quotes at all.
}
:)
回答2:
// Setup browser object
var browser = new IE();
var url = "www.google.com";
browser.GoTo(url);
var searchBox = browser.TextField(Find.ByName("q"));
searchBox.Value="Rex";
//click on the search button
var btnSearch = browser.Button(Find.ByValue("Search"));
btnSearch.Click();
//wait for browser to load properly
browser.WaitForComplete();
// Find the navigation menu table
var navigationtable = browser.Table(Find.ById("nav"));
// To go to the second page
var secondpage = navigationtable.Link(Find.ByText("2"));
secondpage.Click();
//wait for browser to load properly
browser.WaitForComplete();
this simply goes to the second page now if you want to loop through
then
for (int i = 2; i <= 10; i++)
{
var nextpage = navigationtable.Link(Find.ByText("i"));
//check if the link exists
/if yes then click on it
if(nextpage.Exists)
nextpage.click();
browser.waitforComplete
}
来源:https://stackoverflow.com/questions/17502949/error-when-making-a-loop-to-crawl-google-next-page-watin