8

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" />
2
  • 1
    Here's a good reference for using the WebBrowser control (.NET 2.0) to interact with different form elements. Commented Oct 3, 2008 at 20:34
  • It took 10-11 years for that link to die. Internet Archive Commented Dec 2, 2024 at 15:59

9 Answers 9

8

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");
Sign up to request clarification or add additional context in comments.

Comments

8

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

1 Comment

That's great if it were client script I needed, but what I'm trying to do is server side.
2

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

Comments

1

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

2 Comments

This could be a problem if the login page specifically looks for the click of the button
Very true, I had thought about that but decided i'd throw out the invoke method anyway.
1

Have you tried fireEvent instead of RaiseEvent?

Comments

1

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+?

Comments

1

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.

1 Comment

I felt slighty naughty and re-jigged to accept the control document so that it's a generic function.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.