使用webbrowser控件获取javascript onClick函数定义 [英] Get javascript onClick function definition with webbrowser control
问题描述
我正在开发一个用于解析和分析网页的Windows窗体应用程序。我使用了webbrowser控件。该网页有html和javascript代码。我的Windows窗体有c#代码。以下是代码:
HTML代码:
I'm developing a windows form application to parse and analyze web pages. I used webbrowser control. The web page has html and javascript code bellow. and my windows form has c# code. Here is the code:
HTML code:
<div id="div1" >
</div>
<div id="div2">
</div>
Javascript:
Javascript:
function func1(element) {
...
}
document.getElementById("div2").onclick = function foo() { doSomething(); }
导航相关网址后我的Windows窗体应用程序中的C#代码:
C# code in my windows form application after navigating the related URL:
HtmlElement el1 = WebBrowser.Document.GetElementById("div1");
HtmlElement el2 = WebBrowser.Document.GetElementById("div2");
String el1JavaFunc = el1.GetAttribute("onClick");
String el2JavaFunc = el2.GetAttribute("onClick");
el1JavaFunc和el2JavaFunc的结果是 System .__ ComObject 。
我怎样才能获得指定的javascript函数名称?例如,获取el1的func1。
我应该使用任何其他控件而不是WebBrowser吗?
请请注意,我不需要执行事件,我知道System .__ ComObject就像JavaScript函数的包装器。
The result for both el1JavaFunc and el2JavaFunc is System.__ComObject.
How could I get the assigned javascript function name? For instance get func1 for el1.
Should I use any other control instead of WebBrowser?
Please Note that I don't need to execute the events and I know that System.__ComObject is like a wrapper for JavaScript functions.
推荐答案
对于el1:
For el1:
mshtml.IHTMLDOMNode node = el1.DomElement as mshtml.IHTMLDOMNode;
String el1JavaFunc = node.attributes("onclick").value.ToString();
对于el2也可以这样做。除了我认为它不会返回func1;我不明白你为什么会这么想。
见 System .__当我使用getAttribute 时返回ComObject。
And the same can be done for el2. Except I don't think it will return func1; I don't see why you expect that.
See System.__ComObject is returned when I use getAttribute.
这篇关于使用webbrowser控件获取javascript onClick函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!