Cyberherbalist
Cyberherbalist

Reputation: 12329

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

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" />

Upvotes: 8

Views: 29732

Answers (9)

MonsCamus
MonsCamus

Reputation: 143

Just a possible useful extra where the submit button has not been given an Id - as is frequently the case.

    private HtmlElement GetInputElement(string name, HtmlDocument doc) {
            HtmlElementCollection elems = doc.GetElementsByTagName("input");

            foreach (HtmlElement elem in elems)
            {
                String nameStr = elem.GetAttribute("value");
                if (!String.IsNullOrEmpty (nameStr) && nameStr.Equals (name))
                {
                    return elem;
                }
            }
            return null;
    }

So you can call it like so:

 GetInputElement("Login", webBrowser1.Document).InvokeMember("Click");

It'll raise an exception if the submit input with the value 'Login', but you can break it up if you want to conditionally check before invoking the click.

Upvotes: 1

Pashanth
Pashanth

Reputation:

try this button.focus System.Windows.Forms.SendKeys.Send("{ENTER}")

Upvotes: 0

Starwatcher
Starwatcher

Reputation: 842

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");

Upvotes: 8

Kibbee
Kibbee

Reputation: 66162

You posted a comment along the lines of not wanting to use a client side script on @Phunchak's answer. I think what you are trying to do is impossible. The only way to interact with the form is via a client side script. The C# code can only control what happens before the page is sent out to the browser.

Upvotes: 0

Mike C.
Mike C.

Reputation: 4977

You could call the method directly and pass in generic object and EventArgs parameters. Of course, this might not work if you were looking at the sender and EventArgs parameters for specific data. How I usually handle this is to refactor the guts of the method to a doSomeAction() method and the event handler for the button click will simply call this function. That way I don't have to figure out how to invoke what is usually just an event handler to do some bit of logic on the page/form.

In the case of javascript clicking a button for a form post, you can invoke form.submit() in the client side script -- which will run any validation scripts you defined in the tag -- and then parse the Form_Load event and grab the text value of the submit button on that form (assuming there is only one) -- at least that's the ASP.NET 1.1 way with which I'm very familiar... anyone know of something more elegant with 2.0+?

Upvotes: 1

JB King
JB King

Reputation: 11910

Have you tried fireEvent instead of RaiseEvent?

Upvotes: 1

Nikki9696
Nikki9696

Reputation: 6348

There is an example of how to submit the form using InvokeMember here. http://msdn.microsoft.com/en-us/library/ms171716.aspx

Upvotes: 2

EvilEddie
EvilEddie

Reputation: 1035

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

Upvotes: 8

Quintin Robinson
Quintin Robinson

Reputation: 82375

You can try and invoke the Page_ClientValidate() method directly through the clientscript instead of clicking the button, let me dig up an example.

Using MSHTML

mshtml.IHTMLWindow2 myBroserWindow = (mshtml.IHTMLWindow2)MyWebBrowser.Document.Window.DomWindow;
myBroserWindow.execScript("Page_ClientValidate();", "javascript");

Upvotes: 1

Related Questions