-4

This is the button I'm trying to click:

<td class="dark" onclick="document.getElementById('id').value = '0'; document.getElementById('form').submit()">
   test
</td>

I tried this code that uses the Winform WebBrowser control to click on that element:

HtmlElementCollection links = webBrowser1.Document.Links;

foreach (HtmlElement link in links)
{
    if ((link.InnerText != null) && (link.InnerText.Equals("test")))
        link.InvokeMember("Click");
}

It clicks links but not on buttons like the one I posted above. I tried different things like this as well:

if (curElement.GetAttribute("id").Equals("0"))
{
    curElement.InvokeMember("click");
}

What is the correct way to click that table cell from the WebBrowser control?

rene
  • 41,474
  • 78
  • 114
  • 152
kensil
  • 126
  • 1
  • 6
  • 15

1 Answers1

3

The problem is that what you call a 'link' isn't a link in terms of the WebBrowser but a so called tablecell which is in html expressed as a <td> tag which is explained here. That is the reason when you iterate over the document.links collection you don't find what you're looking for.

You can use another method on the Document property of the webbrowsercontrol to get a list of specifc tag names, GetEelementsByTagName. That will give you just <td>'s. With a simple if you can check if you arrived at the correct <td> and then call RaiseEvent on the element to invoke the desired behavior.

  foreach (HtmlElement td in this.webBrowser1.Document.GetElementsByTagName("td")) 
  {
         Debug.WriteLine(td.InnerText);
        if (td.InnerText.Equals("test"))
        {
                     td.RaiseEvent("onclick");
        }
  }
rene
  • 41,474
  • 78
  • 114
  • 152