C# - Waiting for webbrowser

Status
Not open for further replies.

Flip

Old School
Original poster
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
I know I can do

Code:
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
to wait after a webbrowser.Navigate.

Is there a way to wait for the webbrowser after clicking a link like so?

Code:
webBrowser1.Document.GetElementById().InvokeMember("click");
The code above doesn't work because some elements load and trigger the ReadyState.Complete before the whole webpage is loaded. I'm currently using timers, but I'd like a more reliable way if possible.
 

TEBot

Member
member
Joined
Feb 24, 2011
Posts
124
Reaction score
21
Points
15
Location
Auckland, NZ
Website
www.traffic-exchange-bot.com
Yes, you can use the WebBrowser.Navigating or WebBrowser.Navigated events. The Navigating event fires before it navigates and Navigated fires after it navigates. However these two events fire on all frames so if there are multiple frames it can be hard to determine which frame has navigated. The Navigating event exposes the url that it's navigating to, the Navigated event doesn't expose the url.

Hope that helps.
 

Flip

Old School
Original poster
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Yes, you can use the WebBrowser.Navigating or WebBrowser.Navigated events. The Navigating event fires before it navigates and Navigated fires after it navigates. However these two events fire on all frames so if there are multiple frames it can be hard to determine which frame has navigated. The Navigating event exposes the url that it's navigating to, the Navigated event doesn't expose the url.

Hope that helps.

Not quite. I'm not navigating to a new page, rather just clicking a submit button and waiting for the page to load again.
 

TEBot

Member
member
Joined
Feb 24, 2011
Posts
124
Reaction score
21
Points
15
Location
Auckland, NZ
Website
www.traffic-exchange-bot.com
Not quite. I'm not navigating to a new page, rather just clicking a submit button and waiting for the page to load again.
It doesn't matter whether you click a link, submit a form, change the Url property, a script redirects the page or you call the Navigate method, those events will fire.
 

Flip

Old School
Original poster
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
It doesn't matter whether you click a link, submit a form, change the Url property, a script redirects the page or you call the Navigate method, those events will fire.

I don't understand how that helps me, though. The Navigated property fires before the page is fully loaded.

I want to wait for the page to fully load before the code continues.
 

Flip

Old School
Original poster
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Ok, then use the DocumentCompleted event instead.

I want to submit multiple things, though, and I can't really see an efficient way to do that with the DocumentCompleted event.
 

Patel

Veteran
veteran
old school
senior
member
Joined
Aug 12, 2010
Posts
1,593
Reaction score
489
Points
133
Location
Inside Tang
Website
itspatel.com
I want to submit multiple things, though, and I can't really see an efficient way to do that with the DocumentCompleted event.

Use this sub:
Code:
private void waitTillLoad()
{
	WebBrowserReadyState loadStatus = default(WebBrowserReadyState);
	int waittime = 100000;
	int counter = 0;
	while (true) {
		loadStatus = webBrowser1.ReadyState;
		Application.DoEvents();

		if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive)) {
			break; // TODO: might not be correct. Was : Exit While
		}
		counter += 1;
	}
	counter = 0;
	while (true) {
		loadStatus = webBrowser1.ReadyState;
		Application.DoEvents();

		if (loadStatus == WebBrowserReadyState.Complete) {
			break; // TODO: might not be correct. Was : Exit While
		}

		counter += 1;
	}
}

Usage:
Code:
WebBrowser1.Navigate("http://www.google.com/");
waitTillLoad();
Interaction.MsgBox("page is fully loaded!");
 
  • Like
Reactions: Flip

Flip

Old School
Original poster
old school
member
Joined
Jul 23, 2009
Posts
263
Reaction score
69
Points
108
Use this sub:
Code:
private void waitTillLoad()
{
    WebBrowserReadyState loadStatus = default(WebBrowserReadyState);
    int waittime = 100000;
    int counter = 0;
    while (true) {
        loadStatus = webBrowser1.ReadyState;
        Application.DoEvents();

        if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive)) {
            break; // TODO: might not be correct. Was : Exit While
        }
        counter += 1;
    }
    counter = 0;
    while (true) {
        loadStatus = webBrowser1.ReadyState;
        Application.DoEvents();

        if (loadStatus == WebBrowserReadyState.Complete) {
            break; // TODO: might not be correct. Was : Exit While
        }

        counter += 1;
    }
}
Usage:
Code:
WebBrowser1.Navigate("http://www.google.com/");
waitTillLoad();
Interaction.MsgBox("page is fully loaded!");

Thanks dude. I'm pretty sure this is what I needed as it appears to be working.
 

TEBot

Member
member
Joined
Feb 24, 2011
Posts
124
Reaction score
21
Points
15
Location
Auckland, NZ
Website
www.traffic-exchange-bot.com
Use this sub:
Code:
private void waitTillLoad()
{
    WebBrowserReadyState loadStatus = default(WebBrowserReadyState);
    int waittime = 100000;
    int counter = 0;
    while (true) {
        loadStatus = webBrowser1.ReadyState;
        Application.DoEvents();
 
        if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive)) {
            break; // TODO: might not be correct. Was : Exit While
        }
        counter += 1;
    }
    counter = 0;
    while (true) {
        loadStatus = webBrowser1.ReadyState;
        Application.DoEvents();
 
        if (loadStatus == WebBrowserReadyState.Complete) {
            break; // TODO: might not be correct. Was : Exit While
        }
 
        counter += 1;
    }
}

Usage:
Code:
WebBrowser1.Navigate("http://www.google.com/");
waitTillLoad();
Interaction.MsgBox("page is fully loaded!");
That's a bit over the top isn't it?

I want to submit multiple things, though, and I can't really see an efficient way to do that with the DocumentCompleted event.
You know you can unsubscribe from events once they've fired:
Code:
webBrowser1.DocumentCompleted -= webBrowser1_DocumentCompleted;
 

Patel

Veteran
veteran
old school
senior
member
Joined
Aug 12, 2010
Posts
1,593
Reaction score
489
Points
133
Location
Inside Tang
Website
itspatel.com
That's a bit over the top isn't it?


You know you can unsubscribe from events once they've fired:
Code:
webBrowser1.DocumentCompleted -= webBrowser1_DocumentCompleted;

but it works and thats all that matters
 
Status
Not open for further replies.