C# How to Click Button automatically via WebBrowser

Misha picture Misha · Jun 4, 2011 · Viewed 83.5k times · Source

The Html code of my click page is :

<input type="submit" id="publishButton-ns" class="ubtn ubtn-block"
 name="publish" tabindex="10" value="Publish Post">

I tried this code for clicking:

webBrowser1.Document.GetElementById("publishButton-ns").InvokeMember("click");

but this not found the button.

Answer

Ponmalar picture Ponmalar · May 4, 2012

This may help you.

<input type="submit" value="Submit" />

HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");  
foreach (HtmlElement el in elc)  
{  
   if (el.GetAttribute("type").Equals("submit"))  
   {  
        el.InvokeMember("Click");  
   }  
 }