1

I have this HTML code

<div class="anc-style" onclick="window.open('./view.php?a=foo')"></div>

I'd like to extract the contents of the "onclick" attribute. I've attempted to do something like:

div.GetAttribute("onclick").ToString();

Which would ideally yield the string

"window.open('./view.php?a=foo')"

but it returns a System.__ComObject.

I'm able to get the class by changing ("onclick") to ("class"), what's going on with the onclick?

HtmlElementCollection div = webBrowser1.Document.GetElementsByTagName("div");
        for (int j = 0; j < div.Count; j++) {
            if (div[j].GetAttribute("class") == "anc-style") {
             richTextBox1.AppendText(div[j].GetAttribute("onclick").ToString());   
            }
        }
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Durn
  • 35
  • 2
  • 5
  • 2
    What does this have to do with C# – TYY Jan 30 '13 at 22:52
  • Please post the code you have created attempting to accomplish this. – Brian Jan 30 '13 at 22:53
  • Added code to further explain, apologies. It's using webbrowser control. – Durn Jan 30 '13 at 22:59
  • I believe the issue is because the underlying IE engine is returning a "script object" and `GetAttribute` is *not* correctly returning the DOM attribute (but rather the object it represents). While a *total hack*, this would likely be able to be extracted "as a string" with [`HtmlElement.OuterHtml`](http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.outerhtml.aspx). I say again: *total hack*. It may also be possible to extract the text of said "script object" that is returned. But the first step would be to determine exactly *what* type of object it is. –  Jan 30 '13 at 23:05
  • Returning the OuterHtml does work, although in my case this div element also has children so it returns those as text as well. – Durn Jan 30 '13 at 23:10
  • I think this is a duplicate of [System.__ComObject is returned when I use getAttribute](https://stackoverflow.com/questions/9707869/system-comobject-is-returned-when-i-use-getattribute). Use `attributes("onclick").value.ToString()`. – Sam Hobbs Oct 13 '17 at 18:00

2 Answers2

4

You can pull the document tags and extract data such as below using the htmlDocument class. This is only an example

string htmlText = "<html><head></head><body><div class=\"anc-style\" onclick=\"window.open('./view.php?a=foo')\"></div></body></html>";

WebBrowser wb = new WebBrowser();
wb.DocumentText = "";
wb.Document.Write(htmlText);
foreach (HtmlElement hElement in  wb.Document.GetElementsByTagName("DIV"))
{
    //get start and end positions
    int iStartPos = hElement.OuterHtml.IndexOf("onclick=\"") + ("onclick=\"").Length;
    int iEndPos = hElement.OuterHtml.IndexOf("\">",iStartPos);
    //get our substring
    String s = hElement.OuterHtml.Substring(iStartPos, iEndPos - iStartPos);

    MessageBox.Show(s);
}
Sorceri
  • 7,870
  • 1
  • 29
  • 38
1

try also using div[j]["onclick"] what browser are you using?

I've created a jsfiddle that works try out and see if its working for you

http://jsfiddle.net/4ZwNs/102/

Ricky Gummadi
  • 4,559
  • 2
  • 41
  • 67