1

I am running this code:

HtmlElement.GetAttribute("onClick")

To try and get access to the onClick attribute of that element, but all this returns is System.__ComObject. I don't know why this happens, all other GetAttribute calls return the actual string.

Thanks.

TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101

3 Answers3

0

First, you can try to determine type of the control; then cast your object to this type. Accessing properties of element is possible with this method.

For example, if your object is a "div" contains an "onclick" method, you must convert your com object to mshtml.HTMLDivElement (You must add assembly "mshtml.dll" into your project for using mshtml class); then you can look for "onclick" attribute in outerHTML property.

    if (doc.GetElementById("id-of-div").GetAttribute("onclick").Equals("System.__ComObject"))
    {
            mshtml.HTMLDivElement docCOM = (mshtml.HTMLDivElement)doc.GetElementById("id-of-div").DomElement;

            string onClickStr = docCOM.outerHTML.[some string or regex operations here];
    }

-1

You need to use this code:

theElementCollection = WebBrowser1.Document.GetElementsByTagName("A")

For Each curElement As HtmlElement In theElementCollection
    MsgBox(curElement.DomElement.attributes("onclick").value.ToString)
Next
Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
-1

Sample code

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    WebBrowser1.Navigate("http://stackoverflow.com/questions/9707869/system-comobject-is-returned-when-i-use-getattribute")
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Dim a As HtmlElement = WebBrowser1.Document.GetElementById("portalLink").FirstChild
    MsgBox(a.DomElement.attributes("onclick").value.ToString)
End Sub
volody
  • 6,946
  • 3
  • 42
  • 54