System.__ComObject is returned when I use getAttribute

后端 未结 2 1598
北荒
北荒 2020-12-22 06:38

I am running this code:

HtmlElement.GetAttribute(\"onClick\")

To try and get access to the onClick attribute of that element, but all this

相关标签:
2条回答
  • 2020-12-22 07:06

    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
    
    0 讨论(0)
  • 2020-12-22 07:13

    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];
        }
    

    0 讨论(0)
提交回复
热议问题