Search code examples
c#webbrowser-controlgetelementsbytagname

GetElementsByTagName not returning elements in webbrowser control


I am trying to read HTML source using webbrowser control in C#.

The HTML source contains the following line:

input class="myclass" name="commit" type="submit" value="Submit"

I am trying to read the above by using following code:

HtmlElementCollection buttonElement = webBrowser1.Document.GetElementsByTagName("commit");

But it doesn't return any element. I printed buttonElement.Count value and it prints 0.

When I right click in webbrowser control and view source then I can see this element "commit" is there and there is no other element by this name.


Solution

  • It's tag name is input. Therefore your code should be something like this

    var element = webBrowser.Document.GetElementsByTagName("input")
                .Cast<HtmlElement>()
                .Where(e => !String.IsNullOrEmpty(e.GetAttribute("name")) &&  e.GetAttribute("name") == "commit")
                .FirstOrDefault();