Hi All,

I am new to this forum and looking for your help. I am a Software Tester and trying to build a C# program that would be logging in to the website and clicking links, inputing text and loging out.

The code below works to the point where it logs in and clicks first link, but the rest is not executed because of DocumentComplete event. Do I need to use multi-threading to continue clicks after the page loads or is there another technique? Thank you.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using mshtml;
namespace mshtml_automation_demo
{
///<summary>
/// Summary description for Form1.
///</summary>
public class MainForm : System.Windows.Forms.Form
{
private AxSHDocVw.AxWebBrowser axWebBrowser1;
private int Task = 1;
private Button button1;
private Label label1;
private Button button2; // global
///<summary>
/// Required designer variable.
///</summary>
private System.ComponentModel.Container components = null;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///<summary>
/// Clean up any resources being used.
///</summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///<summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///</summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
// 
// axWebBrowser1
// 
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(45, 152);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(616, 382);
this.axWebBrowser1.TabIndex = 0;
this.axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_DocumentComplete);
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(509, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// label1
// 
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(509, 69);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(75, 58);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// MainForm
// 
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(691, 449);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.axWebBrowser1);
this.Name = "MainForm";
this.Text = "Microsoft WebBrowser Automation";
this.Load += new System.EventHandler(this.FrmMain_Load);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
///<summary>
/// The main entry point for the application.
///</summary>
[STAThread]
static void Main() 
{
Application.Run(new MainForm());
}
 
 
public void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
 
HTMLDocument myDoc = new HTMLDocumentClass();
myDoc = (HTMLDocument)axWebBrowser1.Document;
 
HTMLInputElement userName = (HTMLInputElement) myDoc.all.item("username", 0);
userName.value = "test";
HTMLInputElement userPassword = (HTMLInputElement)myDoc.all.item("password", 0);
userPassword.value = "111";
HTMLInputElement btnSearch = (HTMLInputElement) myDoc.all.item("submit1", 0);
btnSearch.click();
HTMLAnchorElement btnSearch2 = (HTMLAnchorElement)myDoc.getElementById("MainMenu");
btnSearch2.click();
HTMLInputElement SearchMe= (HTMLInputElement)myDoc.all.item("SearchMe", 0);
SearchMe.value = "1";
HTMLInputElement btnSearch3 = (HTMLInputElement)myDoc.getElementsByName("SearchButton");
btnSearch3.click();
 

}
 
private void button1_Click(object sender, EventArgs e)
{
object loc = [URL]http://localhost/MySite/[/URL];
object null_obj_str = "";
System.Object null_obj = 0;
this.axWebBrowser1.Navigate2(ref loc, ref null_obj, ref null_obj, ref null_obj_str, ref null_obj_str);
}
}
}
Member Avatar for iamthwee

How exactly do you plan to get your program to automatically identify links and click on them?

How exactly do you plan to get your program to automatically identify links and click on them?

Thanks for you reply!

I specify what to click based on ID or Name tags. It's not a problem. The problem is that after the page loads it clicks all the links at once. Basically what I am trying to do is:

click a link - wait for page to load - input some text - click submit - wait for page to load - click another link.

do I create another thread or another DocumentComplete event so it waits for a 2nd page to load.

Member Avatar for iamthwee

Thanks for you reply!

I specify what to click based on ID or Name tags. It's not a problem. The problem is that after the page loads it clicks all the links at once. Basically what I am trying to do is:

click a link - wait for page to load - input some text - click submit - wait for page to load - click another link.

do I create another thread or another DocumentComplete event so it waits for a 2nd page to load.

Oh right, could you not just use the timer/sleep function to wait for a bit?

This is a block of code that does the trick when you want the webbrowser to wait until the page loads. I registered here just so I could tell you. lol.

My code is in VB.net you can probably trancode it

Do Until Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop

Thanks for you reply!

I specify what to click based on ID or Name tags. It's not a problem. The problem is that after the page loads it clicks all the links at once. Basically what I am trying to do is:

click a link - wait for page to load - input some text - click submit - wait for page to load - click another link.

do I create another thread or another DocumentComplete event so it waits for a 2nd page to load.

What you need to do is check the URL inside DocumentComplete and based on that you perform your tasks. So you check if it's the login page, you input, click. This will redirect you to another page I assume. When the second page finishes loading, DocumentComplete gets invoked again. Check URL, and do whatever clicks. Get it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.