Usually form data is input manually by the user and then a button is clicked to send this form to the server for processing. Sometimes this process needs to be automated. You need to input form data with the help of the code and then click on the submit button programmatically. I’ll show you the code sample to perform this task in visual studio 2005 using C#.
In this article I’ll explain how to login to a web page programmatically. Let say you have a form with user name and password input boxes on it and a login button. You enter user name and password in the form and then click login button, then the application lets you enter into the application password restricted area.
First of all create a Windows Form application in Visual Studio 2005 using C#. Drop a Web Browser control on the form and give it proper height and width, because we’ll access login page in this Web Browser control and it should be visible properly. We're going to show the webpage on the windows form. You can add WebBrowser control from the tool box of the visual studio 2005.
Now, provide the URL of the login form to the WebBrowser control. You need to use navigate method to get the webpage and show it on the windows form. Let say we have given a name wbControl to the dropped web browser control. The following line of code will show the web page on the form.
wbControl.Navigate(“www.yourdomain.com/loginpage”);
You need to find the ids - unique id - of the controls which you want to handle in the code. You can find the ids of the particular controls by viewing the source of the web page in the browser window. Just for your reference when a page is rendered the server side code appends some extra attributes to the control id to make it unique.
Let say we have a user name control with the id “ctl00$ContentPlaceHolder2$Login1$UserName”. We’ll set the user name programmatically as follows.
wbControl.Document.GetElementById("ctl00$ContentPlaceHolder2$Login1$UserName").SetAttribute("value", "yourusername");
The id of our password control is "ctl00$ContentPlaceHolder2$Login1$Password" and following line shows how to set the password in the input box.
webBrowser1.Document.GetElementById("ctl00$ContentPlaceHolder2$Login1$Password").SetAttribute("value", "yourpassword");
Finally, you need to find the id of the button control which is "ctl00$ContentPlaceHolder2$Login1$LoginButton". Now, we have to click the button to login to the page and following line does the work.
webBrowser1.Document.GetElementById("ctl00$ContentPlaceHolder2$Login1$LoginButton").InvokeMember("click");
You’ll see that you’re logged into the application. Write this code in a button’s click event on the windows form.
1 comment:
Hi, thanks for the tutorial. I´´m wondering though if there is a solution for a case like this:
form method="post" action="/login.php5">
input type="image" src="./images/b_login.gif" class="submit" alt="Log In" />
input type="password" name="log[password]" class="input" tabindex="2" />
img src="./images/label_passwort.gif" class="label" alt="Passwort" />
input type="text" name="log[name]" class="input" tabindex="1" />
img src="./images/label_uname.gif" class="label" alt="user name" />
form>
As you see the user logs in by clicking an image which lacks a name/id.
Is there a way to solve this?
Thank you
Post a Comment