Skip to content Skip to sidebar Skip to footer

C# Click HTML Button Without ID On Webbrowser

Im trying to code on c# so a button is clicked on the webbrowser. The button has no ID here is the html:

Solution 1:

You may use the WatiN Library to perform various kinds of web page operation.

First download WatiN and add a reference to it. The code is very simple to implement, you may see the docs here

WatiN is used like this:

public void SearchForWatiNOnGoogle()
{
  using (var browser = new IE("http://www.google.com"))
  {
    browser.TextField(Find.ByName("q")).TypeText("WatiN");
    browser.Button(Find.ByName("btnG")).Click();

    Assert.IsTrue(browser.ContainsText("WatiN"));
  }
}

And in your case, you may use the Class attribute to get the button. There are plenty of samples.


Solution 2:

Using JQuery, that can be done by clicking the button based on the class instead of the id, so for example:

$(document).ready(function() {
    $(".btn").click(function() {
        alert("Bingo")
    })   
})

Solution 3:

You can get the elements by the tag name and then find the exact one you are looking for by looking at the inner text:

var buttons = webBrowser1.Document.GetElementsByTagName("button");

foreach (HtmlElement button in buttons)
{
    if (button.InnerText == "Log me in")
    {
        button.InvokeMember("click");
    }
}

Post a Comment for "C# Click HTML Button Without ID On Webbrowser"