Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i can do this for this web
web
<input id="btnSubmit" class="btn" value="btn">

using this C# code
HtmlDocument doc = this.webBrowser1.Document;
webBrowser1.Document.GetElementById("btnSubmit").InvokeMember("click");

but i can not to do this for
web
<button type="submit" class="btn btn-primary btn-lg">Next</button>

What I have tried:

i want to click web button Automatically in C# win form application for all typ of button
with id or without id
Posted
Updated 9-May-16 19:10pm
v2
Comments
Karthik_Mahalingam 9-May-16 14:24pm    
provide some link

try this

C#
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
           HtmlElementCollection buttons =  webBrowser1.Document.GetElementsByTagName("button");
           if (buttons != null)
           {
               HtmlElement nextButton = buttons.Cast<HtmlElement>().FirstOrDefault(k => k.InnerText == "Next");
               if (nextButton != null)
                  nextButton.InvokeMember("click");
           } 
        }
 
Share this answer
 
Though you have used 'button' type instead of 'input' but when browser render your web page it automatically converts it to 'input', check below snippet and see if it works for you
C#
HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("input");  
foreach (HtmlElement el in elc)  
{  
   if (el.GetAttribute("type").Equals("submit"))  
   {  
        el.InvokeMember("Click");  
   }  
 }

above code collect a tag from HTML source start with 'input' and then check of the type of the tag is submit, if it is. then it will directly Invoke click method.
OR
Other way is to check the view source and copy the 'id' of your web button and used it in below code
C#
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document
        .GetElementById("HTMLID_of_your_web_button")
        .InvokeMember("Click");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900