Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click a button on an ASP.NET web page programmatically?

Tags:

asp.net

I am trying to figure out how to click a button on a web page programmatically.

Specifically, I have a WinForm with a WebBrowser control. Once it navigates to the target ASP.NET login page I'm trying to work with, in the DocumentCompleted event handler I have the following coded:

HtmlDocument doc = webBrowser1.Document;

HtmlElement userID = doc.GetElementById("userIDTextBox");
userID.InnerText = "user1";

HtmlElement password = doc.GetElementById("userPasswordTextBox");
password.InnerText = "password";

HtmlElement button = doc.GetElementById("logonButton");
button.RaiseEvent("onclick");

This fills the userid and password text boxes fine, but I am not having any success getting that darned button to click; I've also tried "click", "Click", and "onClick" -- what else is there?. A search of msdn of course gives me no clues, nor groups.google.com. I gotta be close. Or maybe not -- somebody told me I should call the POST method of the page, but how this is done was not part of the advice given.

BTW The button is coded:

<input type="submit" name="logonButton" value="Login" onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="logonButton" tabindex="4" />
like image 683
Cyberherbalist Avatar asked Sep 18 '08 19:09

Cyberherbalist


2 Answers

How does this work? Works for me

HtmlDocument doc = webBrowser1.Document;

doc.All["userIDTextBox"].SetAttribute("Value", "user1");
doc.All["userPasswordTextBox"].SetAttribute("Value", "Password!");
doc.All["logonButton"].InvokeMember("Click");
like image 56
Starwatcher Avatar answered Oct 26 '22 12:10

Starwatcher


var btn = document.getElementById(btnName); if (btn) btn.click();

like image 25
EvilEddie Avatar answered Oct 26 '22 11:10

EvilEddie