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