C# Auto click button and auto fill form
Posted on February 17th, 2010 in C#, Tutorials | 19 Comments »
This tutorial will show you how to auto fill forms and click buttons in a website using the webBrowser control. When you learn to do this you can make your own web bots!
To show you how autoclick/autofill works we’ll make a simple Google AutoSearch Bot.
So lets begin…
First of all, add a webBrowser control to your form. Set its “Url” property to “www.google.com”.
Now we’ll add two methods SetText() and ClickButton(). SetText() method will automatically fill a textBox and the ClickButton() will click the submit button.
Since we are making a Google AutoSearch Bot we need to find what’s the name of the Google search textBox and the submit button. To find these, visit Google.com from your browser and view the page source.
Here’s the code for the SetText() method:
// Set value for the attribute that has the name (attName)
void SetText(string attribute, string attName, string value)
{
// Get a collection of all the tags with name "input";
HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement currentTag in tagsCollection)
{
// If the attribute of the current tag has the name attName
if (currentTag.GetAttribute(attribute).Equals(attName))
// Then set its attribute "value".
currentTag.SetAttribute("value", value);
}
}
And now the code for the ClickButton() method:
// Click the button whose attribute has a name "attName"
void ClickButton(string attribute, string attName)
{
HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement element in col)
{
if (element.GetAttribute(attribute).Equals(attName))
{
// Invoke the "Click" member of the button
element.InvokeMember("click");
}
}
}
Now that the main methods are added we can now tell the bot what to do
.
Declare “bool searched = false” at Class-level (i.e add it above pulic Form1() {……). We’ll use it to check whether we have already searched or not.
Add DoucmentComplete event for the webBrowser. DocumentComplete event is fired when the page is loaded completely.
Use this code:
Note: The name of the Google search textBox is “q” and the submit buttons is “btnG”.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Check if already searched
if(searched == false)
{
// Set the text to "Search Text Here" of the textBox whose "name" attribute is "q";
SetText("name", "q", "Search Text Here");
// Click the button whose name attribute is "btnG"
ClickButton("name", "btnG");
// Since we have already searched, set it to true
searched = true;
}
}
That’s it! Debug the project and watch the bot
If you have any questions don’t hesitate to post a comment.
Thanks

19 Responses
Awesome. The set value doesnt work for combo-boxes. Which is a bit of a pain since if you did this via javascript, it’s just setting the value for it.
But these ways are much faster/easier then developing using the mshtml library.
Hi,
I am working on a simple application to sign in and submit information to some of the websites. I have a web browser, listbox and textboxes on the form. I have a list of URLs in the listbox and username, password in the textboxes. When I click any item on the listbox, web browser opens that webpage.
Now, I need to fill the related login information and submit. I am having difficulty on this, can you suggest me anything?
Thank you very much…
Thx dude. Im good with php just taking next step to learn c#.
Anyway your explanation is easy to follow and clear keep it up
@Nail: What problem are you facing?
C# is giving me errors on all the " how do i solve this?
Oh sorry! That’s was a mistake in the post. I’ve fixed it now. The code is working perfectly now.
Great job bro,
But you should fix the quotes at the bottom , for example: ClickButton(“name’, ‘btnG”);
Have them be double qoutes , i struggled with this for a full hour as it wont compile with single ones and it throws some crazy errors in.
Everything is working like a charm and your tutotorial is the best!
Kind Regards,
Furio
Thanks Furio. I’ve edited the post.
Doing alot in PHP, but this doesn’t look much different. I think I should give c# a try…can’t be too difficult.
If you are good with Php then you’ll find C#’s syntax much similar.
Hello, for the code, I tried to modify it to work on my website, but it didnt work, and suggestions?
I got it to work, Now. How would I click a span that has a specific text
Like:
You can use this method to click a span:
// The Method:
void ClickSpan(string attribute, string attName)
{
HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName(“span”);
foreach (HtmlElement element in col)
{
if (element.GetAttribute(attribute).Equals(attName))
{
element.InvokeMember(“click”);
}
}
}
// Example use:
ClickSpan(“class”, “yt-uix-button-content”);
Thanks
HELP FOR READING ELEMENTS IN THE AJAX PANEL?!
Hi
thanks, its very use full for me
i have a web application that using ajax panel
i use this method to login the web app.
and going to my goal page, fill the related controls
and finally click the SUBMIT button
after submit, web app. get data from DB and show in the page
IN THE AJAX PANEL
and when i want to read the element that filled after submit, the element is EMPTY!!!
and when i watching it in VIEW SOURCE with browser it also empty BUT it is showing in the monitor???!!!!!!!
please help me
thanks
correction
I using UPDATEPANEL of microsoft Ajax with C# for previous comment
thanks again
Can you post your code, please?
Web App. Code sample
.
.Some controls
. Search BUTTON and TEXT BOX
.
.
.
.
.
I can navigate to this page and fill the search textbox and click the search button with the your sample code (its work fine)
but after that when web app. fill the Label(“APT_NAME”), webBrowser1_DocumentCompleted does not raise
and when I see VIEW SOURCE i found my label here
offcourse i raise it manualy after my search Button clicked
but element value is empty
i try AxWebBrowser and its the same!
is that possible to catch o access elements values where updates with ajax?
thanks
corrected HTML tags ( * for & g t and & l t and \ as slashs)
Web App. Code sample
*asp:ScriptManager ID=”ScriptManager1″ runat=”server”*
*\asp:ScriptManager*
*table width=”100%”*
*tr*
*td align=”center”*
*asp:UpdatePanel ID=”UpdatePanel1″ runat=”server”*
*ContentTemplate*
*table width=”100%”*
.
.Some controls
. Search BUTTON and TEXT BOX
.
*tr
*td
*asp:Label ID=”APT_NAME” runat=”server”* *\asp:Label*
*\td
*\tr
.
.
*\table*
*\ContentTemplate*
*\asp:UpdatePanel*
.
.
I can navigate to this page and fill the search textbox and click the search button with the your sample code (its work fine)
but after that when web app. fill the Label(“APT_NAME”), webBrowser1_DocumentCompleted does not raise
and when I see VIEW SOURCE i found my label here
*td style=”width: 25%; height: 14px;” align=”right”*
*span id=”ctl00_ContentPlaceHolder1_MobileFileInfo2_APT_NAME”* *\span*
*\td*
offcourse i raise it manualy after my search Button clicked
but element value is empty
i try AxWebBrowser and its the same!
is that possible to catch o access elements values where updates with ajax?
thanks