Reputation: 1969
i got a bit of a problem using the WebBrowser DocumentCompleted event. i have this code:
int timesToRun = 20;
private void Time_Tick(object sender, EventArgs e)
{
if (timesToRun >= siteList.SiteLists.Count)
{
timesToRun = 0;
webBrowser1.DocumentCompleted -= this.webBrowser1_DocumentCompleted;
Console.WriteLine("timer is done");
timer.Stop();
}
timer.Enabled = false;
Console.WriteLine("timer is now disabled");
Console.WriteLine(timesToRun);
string currentSite = siteList.GetSiteFromIndex(timesToRun);
webBrowser1.Navigate(currentSite);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
if (e.Url.AbsolutePath != wb.Url.AbsolutePath) //somtimes dosn't return true
{
Console.WriteLine(wb.Url);
forumAction.FillOutFormIn(wb.Document);
timesToRun++;
Console.WriteLine("timer is now enabled");
timer.Enabled = true;
}
}
my problem that the if statment is true only somtimes. im not sure what more can i check to be sure the browswer is ready to use.
what else can i check? or should i take a diffrent approch to this problem?
i got this if statment from a diffrent question here in stackOverFlow becuse i hed a problem that the event was fired more the onces (becuse iframe's and stuff).
(sorry for my english)
Upvotes: 0
Views: 1659
Reputation: 5452
I think the problem is that the url of the browser can differ from the url of a nested IFrame, so when you call
e.Url.AbsolutePath != wb.Url.AbsolutePath
if the DocumentCompleted
event is triggered by an IFrame, then the url in e.Url
is the one of the IFrame while wb.Url
is the url of the containing page in the webbrowser.
If you need to check if the browser is ready to use you might have to wait a long time. You can loop through Frames
property of (WB1.Document.Window.Frames
) to see if the loading is completed.
You could use external libraries like watin
, it has a good check on document completion.
Upvotes: 3