| | |
How to click links on the backgroun
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2006
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
How exactly do you plan to get your program to automatically identify links and click on them?
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.
•
•
•
•
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.
*Voted best profile in the world*
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
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
My code is in VB.net you can probably trancode it
C# Syntax (Toggle Plain Text)
Do Until Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Application.DoEvents() Loop
•
•
Join Date: Apr 2009
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Similar Threads
- can't right click, install active-x, download from any site, click on links, etc (Viruses, Spyware and other Nasties)
- Pay Per Click Links (Ad Space for Sale)
- Can't click links or my pc freezes.Please help! (Windows NT / 2000 / XP)
- I CANT CLICK ON LINKS! (Web Browsers)
- cant click on links. ? why? (Web Browsers)
Other Threads in the C# Forum
- Previous Thread: Decoding problems
- Next Thread: Convert to a type
| Thread Tools | Search this Thread |
.net access algorithm animation array asp avltree bitmap box c# check checkbox client column combobox control conversion csharp database datagrid datagridview datagridviewcheckbox dataset datetime degrees directrobot display draganddrop drawing encryption enum equation excel file form format formatting formbox forms formupdate function gdi+ hash image input install java leak linux list math mouseclick mp3 mysql namevaluepairs native networking operator packaging path photoshop picturebox pixelinversion post powerpacks print process programming radians regex remoting reporting richtextbox robot safari server sleep snooze socket sql statistics string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser wfa winforms wpf xml






