Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


How to get "class" attribute of an HTML element.


class="button"

How do you do this do you know

i know how to get an element by id, but not class
Posted
Comments
Sergey Alexandrovich Kryukov 29-Jun-12 13:57pm    
It sounds like you could get an element by id using JavaScript, not VB. Is it ASP.NET or what? Please explain more, if you really need an advice.
--SA
fatcake 29-Jun-12 14:02pm    
it is visual basic i am doing it in,

normally you would do to get an element by id by doing

webbrowser1.document.getelementbyid("ID")

Any ideas, do you still need more info what?
Sergey Alexandrovich Kryukov 29-Jun-12 14:27pm    
Yes. Are you talking about System.Windows.Forms.WebBrowser, System.Windows.Controls.WebBrowser (used in WPF) or something else? The difference is important. Please always, from the very beginning, explain what do you use in fully qualified names. Maybe there are more different variants then you can be aware of, so please be careful.
--SA
fatcake 29-Jun-12 14:32pm    
System.Windows.Forms.WebBrowser
fatcake 29-Jun-12 14:34pm    
bit more html to help :)

<input id="checkout_submit" class="button" type="submit"

The formal answer is this: you can always get an element by id, because the id is always unique. As to the HTML class, many elements can use the same class. So, the method you are looking for cannot exist.

This does not suggest you any definitive course of action. Probably it could be possible if you explain what do you want to achieve, why…

[EDIT]

For example, if you are working with System.Windows.Forms.HtmlDocument, you could traverse all available elements starting with System.Windows.Forms.HtmlDocument.All or get a collection of elements by System.Windows.Forms.HtmlDocument.GetElementsByTagName (if you only need an element of known tag) and, for each element, check its class attribute using System.Windows.Forms.HtmlElement.GetAttribute. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.document.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.aspx[^].

Pretty easy. And don't think that if you had a ready-to-use method "GetElementByClass" returning the collection of elements, it would be faster. If you write your method doing what you want with the element of certain class, it would be just right.

If you need something else, or if you use different library, please explain. Thank you for clarification; it looks like my recipe for System.Windows.Forms.HtmlDocument will work for you.

—SA
 
Share this answer
 
v4
Since class is a reserved word, Microsoft used className instead of class, had same issue in C#
 
Share this answer
 
Comments
xi0n 15-Jan-21 11:58am    
This solution was the simple answer I needed:
OurClassName = Element.GetAttribute("className")

Even though it's class in the HTML, when in .NET you need to use 'className' as the attribute name.

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