WebBrowser (loop automation)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

Background information ….

I have a VS2005 C# Windows forms app which has several text fields, one
button control and one webbrowser control that I use to display an external
webpage and based on the input data provided by the user, I perform some
tasks on the webpage. The idea is to go automatically go through several web
pages based in data captured by the user until I reach to a desired webpage.

All this works fine,
The user fills the data on the windows form, then clicks on the form's
button and the process starts.
The click method of this button does something like like …

private void btnSendData_Click(object sender, EventArgs e)
{

Uri myUri = new Uri (http://www.externalwebsite/page1.htm);
this.myWebBrowser.Url = myUri;
this.stageSecuence = 1;

}

I use a variable named "stageSecuence" in the next method to perform some
automated tasks depending on the webpage that is being processed.

Then for the webbrowser control I have the following method:

private void myWebBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{

switch (this.stageSecuence)
{
case 1:
// perform some tasks like invoking the submit on the web page
this.stageSecuence++;
case 2:
// perform some other tasks
this.stageSecuence++;

// and so on

}

Current situation,

Now, instead of executing this automation on the webpage based on the data
manually typed by the user; I want to call this process several times based
on some database information.
I wrote the following code to perfom a loop (hoping to call sereval times
the automation process)

for (int i = 0; i < this.numberOfTableRecords; i++)
{
// get record info

Uri myUri = new Uri (http://www.externalwebsite/page1.htm);
this.myWebBrowser.Url = myUri;
this.stageSecuence = 1;
}

By doing this I only get the myWebBrowser_DocumentCompleted method executed
only once for the last record of the loop; in fact this method is executed
once the loop has finished.

How can I get the DocumentCompleted method executed once for every record in
the loop ?
Can I call this method manually such as:
this.myWebBrowser_DocumentCompleted( … ) ??

Thanks in advance,
Cesar
 
You have to set up an event driven "loop" if you will.
You won't be able to use a for loop.

Essentially, you'll have class level private variables that hold
the page you are on and an array of pages you need to process.

You write a method that processes the steps. When the user
hits a button, you fire step 1. Then, in the document complete
event, you determine the page just processed and call the method
to process the next page. You let the document complete event
drive the process not your for loop.

You could get fancier than this of course but it should suffice.

I wrote something like this to auto submit letters to ever member
of congress through their own individual web sites and custom
authentication schemes.

--
Robbe Morris [Microsoft MVP - Visual C#]
..NET PropertyGrid Control - ListBox, ComboBox, and Custom Classes
http://www.eggheadcafe.com/tutorial...af-5cd3abe27a75/net-propertygrid-control.aspx
 
Back
Top