Click here to Skip to main content
15,741,947 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
How to get "class" attribute of an HTML element.
For example, for this HTML code, this C# code does well:
hElement.GetAttribute("id")  // Results "target_validate"

But it does not work for "class" attribute:
hElement.GetAttribute("class")  // Results ""

I use C# WebBrowser control.

<table id="target_validate" class="res_target" cellpadding="1" cellspacing="1">
		<td class="vil" colspan="2">AVATAR (18|60)</td>
		<td><a href="spieler.php?uid=4018">Iman</a></td>
Updated 2-Aug-18 22:24pm

user "className' instead of "class"

like GetAttribute("className")
Share this answer
Prathyangira's Karthikeyan 3-Sep-15 1:27am    
wow best answer i like that thankyou from Prathyangira`s Karthikeyan (WEb Desinger) india

I have no idea why, and doesnt make sense with what is actually in the html, but instead of htmlelement.GetAttribute("class"), use htmlelement.GetAttribute("classname"). I have it implemented as such in my app and works perfect.
Share this answer
the following code will return "res_target"

Share this answer
Guess you are using some C# methods to access the HTML DOM. Would be useful to know which library you are using (is that built into .Net?). Anyway, I imagine this explains why it isn't working:

Basically, you should use "obj.className" rather than "obj.getattribute('className')". Not sure if there is a corresponding function on whatever class "hElement" is. If not, then perhaps you can call a JavaScript function that just returns the result of "obj.className". You'd embed that JavaScript function in the HTML page you are calling, then call that JavaScript function from C# (again, I'd have to know what library you are using).
Share this answer
how to access to "display" style type (hide, block, ...) in webbrowser?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900