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.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,821 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,199 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 111.8K 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