<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Farooq Azam &#187; auto click</title>
	<atom:link href="http://www.farooqazam.net/tag/auto-click/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.farooqazam.net</link>
	<description>C# &#38; .NET Programming Blog</description>
	<lastBuildDate>Fri, 23 Jul 2010 20:15:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C# Auto click button and auto fill form</title>
		<link>http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/</link>
		<comments>http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 11:58:39 +0000</pubDate>
		<dc:creator>Farooq Azam</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[auto click]]></category>
		<category><![CDATA[auto fill]]></category>
		<category><![CDATA[auto search]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.farooqazam.net/?p=162</guid>
		<description><![CDATA[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&#8217;ll make a simple Google AutoSearch Bot. So lets begin&#8230; First of all, add a webBrowser [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px; margin-top: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-auto-click-button-and-auto-fill-form%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.farooqazam.net%2Fc-sharp-auto-click-button-and-auto-fill-form%2F&amp;source=farooqaaa&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a title="Goolge AutoSearch Bot" href="http://www.farooqazam.net/wp-content/uploads/2010/02/da-app.gif"><img class="alignnone size-full wp-image-176" title="da-app" src="http://www.farooqazam.net/wp-content/uploads/2010/02/da-app.gif" alt="da-app" width="473" height="427" /></a></p>
<p>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!</p>
<p>To show you how autoclick/autofill works we&#8217;ll make a simple Google AutoSearch Bot.</p>
<p>So lets begin&#8230;<span id="more-162"></span></p>
<p>First of all, add a <em>webBrowser</em> control to your form<em>.</em> Set its <em>&#8220;Url&#8221; </em>property to &#8220;www.google.com&#8221;.</p>
<p>Now we&#8217;ll add two methods <em>SetText()</em> and <em>ClickButton()</em>.<em> SetText()</em> method will automatically fill a textBox and the <em>ClickButton()</em> will click the submit button.</p>
<blockquote><p>Since we are making a Google AutoSearch Bot we need to find what&#8217;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.</p></blockquote>
<p>Here&#8217;s the code for the SetText() method:</p>
<pre class="brush: csharp;">// 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 &quot;input&quot;;

 HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName(&quot;input&quot;);

 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 &quot;value&quot;.

   currentTag.SetAttribute(&quot;value&quot;, value);
 }
}
</pre>
<p>And now the code for the ClickButton() method:</p>
<pre class="brush: csharp;">// Click the button whose attribute has a name &quot;attName&quot;

 void ClickButton(string attribute, string attName)
 {
    HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName(&quot;input&quot;);

    foreach (HtmlElement element in col)
    {
      if (element.GetAttribute(attribute).Equals(attName))
      {

      // Invoke the &quot;Click&quot; member of the button
      element.InvokeMember(&quot;click&quot;);
      }
    }
 }
</pre>
<p>Now that the main methods are added we can now tell the bot what to do <img src='http://www.farooqazam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Declare &#8220;bool searched = false&#8221; at Class-level (i.e add it above pulic Form1() {&#8230;&#8230;). We&#8217;ll use it to check whether we have already searched or not.</p>
<p>Add <em>DoucmentComplete</em> event for the webBrowser. DocumentComplete event is fired when the page is loaded completely.</p>
<p>Use this code:</p>
<p><em>Note: The name of the Google search textBox is &#8220;q&#8221; and the submit buttons is &#8220;btnG&#8221;.</em></p>
<pre class="brush: csharp;">
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {

 // Check if already searched

 if(searched == false)
 {

 // Set the text to &quot;Search Text Here&quot; of the textBox whose &quot;name&quot; attribute is &quot;q&quot;;

 SetText(&quot;name&quot;, &quot;q&quot;, &quot;Search Text Here&quot;);

 // Click the button whose name attribute is &quot;btnG&quot;

 ClickButton(&quot;name&quot;, &quot;btnG&quot;);

 // Since we have already searched, set it to true

 searched = true;

 }
 }
</pre>
<p>That&#8217;s it! Debug the project and watch the bot <img src='http://www.farooqazam.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>If you have any questions don&#8217;t hesitate to post a comment.</p>
<p>Thanks</p>
]]></content:encoded>
			<wfw:commentRss>http://www.farooqazam.net/c-sharp-auto-click-button-and-auto-fill-form/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
	</channel>
</rss>
