title: C# Target HTML button by class
body: <p>I want to click on a button which doenst have an ID. I used <a href="https://stackoverflow.com/questions/24986652/c-sharp-click-html-button-without-id-on-webbrowser">this</a> stackoverflow question to get started, but I can't seem to make it work.</p>
  <pre><code>        //Load page 
          WebBrowser webBrowser1 = new WebBrowser();
          webBrowser1.Navigate(&quot;/login&quot;);
          while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
              System.Windows.Forms.Application.DoEvents();
  
          //Log in
  
          HtmlDocument doc = webBrowser1.Document;
          doc.GetElementById(&quot;username&quot;).SetAttribute(&quot;Value&quot;, &quot;--&quot;);
          doc.GetElementById(&quot;password&quot;).SetAttribute(&quot;Value&quot;, &quot;--&quot;);
          doc.GetElementById(&quot;_submit&quot;).InvokeMember(&quot;Click&quot;);
  
  
          WebBrowser webBrowser2 = new WebBrowser();
          webBrowser2.Navigate(&quot;/Start&quot;);
          while (webBrowser2.ReadyState != WebBrowserReadyState.Complete)
             System.Windows.Forms.Application.DoEvents();
  
  
          var buttons = webBrowser2.Document.GetElementsByTagName(&quot;button&quot;);
         Console.WriteLine(buttons); **//Output here: 'System.Windows.Forms.HtmlElementCollection'**
          foreach (HtmlElement button in buttons)
          {
              
              if (button.InnerText == &quot;Filter    &quot;)
              {
                  
                  button.InvokeMember(&quot;Click&quot;);
                  Console.WriteLine(&quot;Clicked!&quot;); **// This will never get writing into the console.**
              }
          }
  
  
  }
  </code></pre>
  <p>The HTML:</p>
  <pre><code>    &lt;button class=&quot;down-arrow-btn&quot; type=&quot;button&quot; id=&quot;toggleFilterFieldsButton&quot;
          title=&quot;Overige filtervelden tonen/verbergen&quot;&gt; ▼
  &lt;/button&gt;
  &lt;button class=&quot;filter-btn&quot; type=&quot;submit&quot; name=&quot;filter_action&quot; title=&quot;Filteren&quot;
          style=&quot;color: #7989a0&quot;
          value=&quot;filter&quot;&gt;Filter
  &lt;/button&gt;
  &lt;button class=&quot;herstel-btn&quot; type=&quot;submit&quot; name=&quot;filter_action&quot; title=&quot;Filteren opheffen, alles tonen&quot;
          value=&quot;reset&quot;&gt;Herstel
  &lt;/button&gt;
  </code></pre>
  <p>In this case I want to press the second button.</p>
  <p><strong>Edit:</strong></p>
  <p><strong>First solution:</strong></p>
  <pre><code>             var element = webBrowser2.Document.GetElementsByTagName(&quot;button&quot;)
          .Cast&lt;HtmlElement&gt;()
          Where(e =&gt; !String.IsNullOrEmpty(e.GetAttribute(&quot;name&quot;)) &amp;&amp; e.GetAttribute(&quot;name&quot;) == &quot;filter_action&quot;)
          .FirstOrDefault();
         
  </code></pre>
  <p><strong>Second solution:</strong></p>
  <pre><code>         var elementToClick = webBrowser2.Document
          .GetElementsByTagName(&quot;button&quot;)
          .Cast&lt;HtmlElement&gt;()
          .FirstOrDefault(m =&gt; m.GetAttribute(&quot;className&quot;) == &quot;filter-btn&quot;);
          elementToClick.InvokeMember(&quot;Click&quot;);
          Console.WriteLine(elementToClick);
  </code></pre>
  <p>I have tried multiple other solutions. But the htmlElement is always null.
  I think the problem is due to the website being pretty secured. And the button I am targeting is not visible for the program? Even tho it's visible in the code.<br />
  If I copy the whole HTML code (view-source:https:// url ) and make it an local .html, it works fine. (all of the 3 solutions.)</p>