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 Go
Trang 1C# Auto click button and auto fill form
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:
view source
Trang 2// 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"
Trang 3void 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)
{
Trang 4// 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
You can use this method to click a span:
Trang 5// 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
How can I fill combo box using this code? or is there any other code that I can use to fill combo box ?
Ok
I got this work Wrote a new function to fill values in Combo boxes and works like a charm Thanks dude
Here is the code
public void SetComboText(string attribute, string attName, string value)
{
// Get a collection of all the tags with name “input”;
HtmlElementCollection tagsCollection =
webBrowser.Document.GetElementsByTagName(“select”);
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);
}
}
Trang 6I got this work Wrote a new function to fill values in Combo boxes and works like a charm Thanks dude
Here is the code
public void SetComboText(string attribute, string attName, string value)
{
// Get a collection of all the tags with name “input”;
HtmlElementCollection tagsCollection =
webBrowser.Document.GetElementsByTagName(“select”);
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);
}
}