C# Getting Attribute Value from HtmlElement

Rico Strydom 1 Reputation point
2022-09-06T07:53:53.473+00:00

I am getting an HtmlElement from a click.

HtmlElement element = WebBrowser.Document.GetElementFromPoint(e.ClientMousePosition);  

This neatly returns an

HtmlElement
with Id (loginUserName), OuterHTML (<input class="form-control ng-pristine ng-untouched ng-isolate-scope ng-invalid ng-invalid-required" id="loginUserName" required="" type="text" placeholder="Username" ng-enter="ok()" ng-model="user.username" surec-focus="true">) etc.

When I try to get the value of the "class" attribute with ...

string classNameValue = element.GetAttribute("class");  

... it returns an empty string.

Any idea why the method is unable to retrieve the attribute value?

Bizarre part is that ...

string elementId = element.GetAttribute("id");  

and

string elementType = element.GetAttribute("type");  

...do return values.

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-09-06T08:44:34.627+00:00

    It seems that the class can be obtained using a special attribute name: element.GetAttribute( "className" ).

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.