943,774 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 11146
  • C# RSS
Nov 14th, 2006
0

How to click links on the backgroun

Expand Post »
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 = http://localhost/MySite/;
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);
}
}
}
Last edited by klas; Nov 14th, 2006 at 2:53 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
klas is offline Offline
2 posts
since Nov 2006
Nov 14th, 2006
0

Re: How to click links on the backgroun

How exactly do you plan to get your program to automatically identify links and click on them?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 14th, 2006
0

Re: How to click links on the backgroun

Click to Expand / Collapse  Quote originally posted by iamthwee ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
klas is offline Offline
2 posts
since Nov 2006
Nov 14th, 2006
0

Re: How to click links on the backgroun

Click to Expand / Collapse  Quote originally posted by klas ...
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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 17th, 2009
0

Re: How to click links on the backgroun

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
C# Syntax (Toggle Plain Text)
  1. Do Until Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete
  2. Application.DoEvents()
  3. Loop
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Idkfawin32 is offline Offline
1 posts
since Feb 2009
Apr 4th, 2009
0

Re: How to click links on the backgroun

Click to Expand / Collapse  Quote originally posted by klas ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fnky is offline Offline
1 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Decoding problems
Next Thread in C# Forum Timeline: Convert to a type





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC