C# Getting Attribute Value from HtmlElement

314 views Asked by At

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");

...does return a value.

1

There are 1 answers

2
UserNam3 On

If you are using HTMLAgilityPack, tt's normal, you get the "attribute" object, which is composed of a name and a value.

Just do this :

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