DocumentCompleate event after clicking a button

O

ofiras

Hi everyone,
I am working on a program in C# and I have a problem.
I am using WebBrowser, and I need the program to click a button that
will transfer me to another URL, and then I need an event that will
fire after the WebBrowser will finish navigating.
The problem is when I try getting this event. When I use
DocumentCompleate it just fires right away, without waiting for the
WebBrowser to navigate. I thought that I could just take the URL the
button is navigating to, but I can't since it navigates by
JavaScript.
So what I have to do is to find out when the WebBrowser is starting to
navigate away, and then add the DocumentCompleate event.
How can I do it?
Please help,
Ofir.
 
P

Peter Duniho

ofiras said:
Hi everyone,
I am working on a program in C# and I have a problem.
I am using WebBrowser, and I need the program to click a button that
will transfer me to another URL, and then I need an event that will
fire after the WebBrowser will finish navigating.
The problem is when I try getting this event. When I use
DocumentCompleate it just fires right away, without waiting for the
WebBrowser to navigate. I thought that I could just take the URL the
button is navigating to, but I can't since it navigates by
JavaScript.
So what I have to do is to find out when the WebBrowser is starting to
navigate away, and then add the DocumentCompleate event.
How can I do it?

There are other events, like Navigating, that inform you as to the
progress of navigation at early steps.

Without a concise-but-complete code example from you showing exactly
what you're doing, it's impossible to offer anything more specific.
There are just too many possibilities to anticipate exactly what problem
you're having.

Pete
 
O

ofiras

There are other events, like Navigating, that inform you as to the
progress of navigation at early steps.

Without a concise-but-complete code example from you showing exactly
what you're doing, it's impossible to offer anything more specific.
There are just too many possibilities to anticipate exactly what problem
you're having.

Pete

Thanks for the comment.
I've tried every event that seems fit, and even all together, but it
still didn't work.
This is the code:

private bool click_input(HtmlElement input)
{
input.InvokeMember("click");
webBrowser1.ProgressChanged += new
System.Windows.Forms.WebBrowserProgressChangedEventHandler
(webBrowser1_ProgressChanged);
}

private void webBrowser1_ProgressChanged(object sender,
WebBrowserProgressChangedEventArgs e)
{
webBrowser1.ProgressChanged -= new
System.Windows.Forms.WebBrowserProgressChangedEventHandler
(webBrowser1_ProgressChanged);
webBrowser1.DocumentCompleted += new
System.Windows.Forms.WebBrowserDocumentCompletedEventHandler
(this.webBrowser1_DocumentCompleted1);
}

private void webBrowser1_DocumentCompleted1(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.DocumentCompleted -= new
System.Windows.Forms.WebBrowserDocumentCompletedEventHandler
(this.webBrowser1_DocumentCompleted1);
do_something();
}


I've tried it with other event, such as Navigated and Navigating, but
it still fired immediately, without waiting for the page to be fully
loaded.
Thanks,
Ofir.
 
P

Peter Duniho

ofiras said:
I've tried every event that seems fit, and even all together, but it
still didn't work.
This is the code:

You may find these links helpful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/
[...]
I've tried it with other event, such as Navigated and Navigating, but
it still fired immediately, without waiting for the page to be fully
loaded.

Again, without a complete code example it's not possible to provide
specific suggestions.

That said, keep in mind that "download complete" is relative. To the
browser control it simply means all of the static content has been
downloaded. But if the page has Javascript, a plug-in, etc. that
downloads additional content dynamically, the browser control can't
provide your code with feedback on that status.

Pete
 
O

ofiras

ofiras said:
I've tried every event that seems fit, and even all together, but it
still didn't work.
This is the code:

You may find these links helpful:http://www.yoda.arachsys.com/csharp...s.com/csharp/incomplete.htmlhttp://sscce.org/
[...]
I've tried it with other event, such as Navigated and Navigating, but
it still fired immediately, without waiting for the page to be fully
loaded.

Again, without a complete code example it's not possible to provide
specific suggestions.

That said, keep in mind that "download complete" is relative.  To the
browser control it simply means all of the static content has been
downloaded.  But if the page has Javascript, a plug-in, etc. that
downloads additional content dynamically, the browser control can't
provide your code with feedback on that status.

Pete

Well, I need the same result that DocumentCompleate gives me.
As I said, the only problem is that the browser doesn't immediately
start navigating to another URL after I click the button, therefore if
I add DocumentCompleate event after I click, it fires immediately.
This happens since the page is fully loaded with the first URL (before
I click) and the web browser is on "Ready" state, and only after a few
second it starts navigating.
So I need to know when the WebBrowser is navigating away, and only
then add the DocumentCompleate event. I've tries using Navigating
event, but it also fires immediately (event if I wait for Navigating,
than DocumentCompleate).
The code I posted is the only code relevant to my problem.
Thanks,
Ofir.
 
P

Peter Duniho

ofiras said:
[...]
The code I posted is the only code relevant to my problem.

Frankly, that's simply not true. I already posted links you should read
to understand why.
 
O

ofiras

ofiras said:
[...]
The code I posted is the only code relevant to my problem.

Frankly, that's simply not true.  I already posted links you should read
to understand why.

Sorry, I just read your post and forgot to enter the links.
Here is the full code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace My_Program
{
class Class2
{
string input_name;
private void button1_Click(object sender, EventArgs e)
{
input_name = "INPUT_NAME_HERE";
start_process();
}

private void start_process()
{
HtmlElementCollection inputs_collection =
webBrowser1.Document.GetElementsByTagName("input");
int i = 0;
bool is_found = false;
HtmlElement input;
while (inputs_collection.Count >= i + 1 && !is_found) //
Find the input I am looking for.
{
input = inputs_collection;
if (input.Name.IndexOf(input_name) != -1)
if (click_input(input)) is_found = true;
i++;
}
if (!is_found) MessageBox.Show("Finished!");
}

private void click_input(HtmlElement input)
{
input.InvokeMember("click");
webBrowser1.ProgressChanged += new
System.Windows.Forms.WebBrowserProgressChangedEventHandler
(webBrowser1_ProgressChanged);
}

private void webBrowser1_ProgressChanged(object sender,
WebBrowserProgressChangedEventArgs e)
{
webBrowser1.ProgressChanged -= new
System.Windows.Forms.WebBrowserProgressChangedEventHandler
(webBrowser1_ProgressChanged);
this.webBrowser1.DocumentCompleted += new
System.Windows.Forms.WebBrowserDocumentCompletedEventHandler
(this.webBrowser1_DocumentCompleted);
}

private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.DocumentCompleted -= new
System.Windows.Forms.WebBrowserDocumentCompletedEventHandler
(this.webBrowser1_DocumentCompleted);
do_something(); // This should happen after page is fully
loaded
}
}
}



As you can see, I am going through all of the inputs in the page,
finding the input I want (it is a button), click it, and wait for the
page to finish navigating.
The last part is my problem - knowing when the page finished
navigating. The code I posted fires DocumentCompleted immediately.
Thanks,
Ofir.
 
P

Peter Duniho

ofiras said:
ofiras said:
[...]
The code I posted is the only code relevant to my problem.
Frankly, that's simply not true. I already posted links you should read
to understand why.

Sorry, I just read your post and forgot to enter the links.
Here is the full code:

Please go re-read the links I provided again. You did _not_ post a
complete code example. Not the least of which reason is that you failed
to provide any sample data against which to test the code (i.e. a URL
known to reproduce the issue you're asking about), never mind that the
code itself is incomplete and contains at least one compilation error
not even counting the other missing parts.

As for the code you did post, I will reiterate my previous statement:
you should have the events subscribed before any action is taken.
Subscribing dynamically as things appear to occur isn't guaranteed to be
reliable. Whether that has anything to do with your specific question,
I don't know (I can't know until you post a proper concise-but-complete
code example), but you still need to fix it.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top