如何从浏览器控件获取 onclick 中的实际 JavaScript 值?
我正在寻找一种方法来获取 onclick 内部定义的 JavaScript 代码。 我使用的是 .NET 2.0 C# Visual Studio 2005。
示例:
<span id="foo" onclick+"window.location.href='someURL'>click here</span>
我的目标是获取字符串“window.location.href='someURL'”。
场景:
用户单击 WebBrowser 控件内部的网页元素(例如上面显示的标记)。然后单击的标签将引用到 HtmlElement 对象
。
然后在 WebBrowser 控件中,我调用 HtmlElement 对象的 getAttribute("onclick")
,它只给我“System.__ComObject”。
我搜索了如何处理它,然后发现可以对其进行转换然后获取值。
if (tag.GetAttribute("onclick").Equals("System.__ComObject"))
{
Console.WriteLine("dom elem >>>>>>>>>>> " + tag.DomElement.ToString());
mshtml.HTMLSpanElementClass span = (mshtml.HTMLSpanElementClass)tag.DomElement;
Console.WriteLine("js value ===>" + span.onclick);
}
输出:
dom elem >>>>>>>>>>> mshtml.HTMLSpanElementClass
js value ===> System.__ComObject
如图所示,span.onclick仍然给我System.__ComObject,我做错了什么?
为什么 HtmlElement 的 GetAttribute() 方法返回“mshtml.HTMLInputElementClass” ”而不是属性的值?这家伙说它在他的情况下有效,我已经遵循了它,但我的有点不起作用......
更新
研究,研究.....
我可以将引用 VisualBasic.dll 添加到我的 C# 项目中,然后调用该方法来找出这个 System.__ComObject 到底是谁。
Console.WriteLine(Microsoft.VisualBasic.Information.TypeName(span.onclick));
输出:
JScriptTypeInfo
看起来这是一个 JScript 类型...我如何访问这个对象?
更多细节
以上描述基于我当前的项目。该项目旨在创建类似 Selenium IDE 的东西。它使用 WebBrowser 控件代替。
Selenium IDE 创建 3 种不同的东西来记录 Web 文档中的元素。
1. actionType
2. xpath
3. value
例如,
type, //input[@id=foo], "hello world"
clickAndWait, //link=login, ""
Selenium IDE 可以识别页面加载,因此它可以在 "click"
和 "clickAndWait"
之间更改 actionType。我的情况,我想让它变得简单。
如果我点击该元素并且它是锚标记
或具有页面加载类型的javascript 例如 onclick=window.location.href='blah'
然后我想将 actionType 设置为 "clickAndWait"
。
I'm looking for a way to get the JavaScript code defined inside of onclick.
I'm using .NET 2.0 C# Visual Studio 2005.
Example:
<span id="foo" onclick+"window.location.href='someURL'>click here</span>
My goal is to get the string "window.location.href='someURL'".
Scenario:
A user clicks on web page element, the tag shown above for instance, inside of WebBrowser control. Then the clicked tag is refereed to HtmlElement object
.
In WebBrowser control I then call HtmlElement object's getAttribute("onclick")
, it just gives me "System.__ComObject".
I've searched how to deal with it then found that it can be casted then get the value.
if (tag.GetAttribute("onclick").Equals("System.__ComObject"))
{
Console.WriteLine("dom elem >>>>>>>>>>> " + tag.DomElement.ToString());
mshtml.HTMLSpanElementClass span = (mshtml.HTMLSpanElementClass)tag.DomElement;
Console.WriteLine("js value ===>" + span.onclick);
}
Output:
dom elem >>>>>>>>>>> mshtml.HTMLSpanElementClass
js value ===> System.__ComObject
As it shown, span.onclick still give me System.__ComObject, what am I doing wrong?
In Why does HtmlElement's GetAttribute() method return “mshtml.HTMLInputElementClass” instead of the attribute's value? this guy said it worked in his case, and I've followed it, but mine is somewhat not working...
UPDATE
Research, research.....
I can add reference VisualBasic.dll to my C# project then call the method to find out who is this System.__ComObject really is.
Console.WriteLine(Microsoft.VisualBasic.Information.TypeName(span.onclick));
Output:
JScriptTypeInfo
It looks like this is a JScript type... how can I access this object?
More detail
The above description is based on my current project. The project is to create something like Selenium IDE. It uses WebBrowser control instead.
Selenium IDE creates 3 different things to record an element in the web document.
1. actionType
2. xpath
3. value
For instance,
type, //input[@id=foo], "hello world"
clickAndWait, //link=login, ""
Selenium IDE recognize page load so it changes actionType between "click"
and "clickAndWait"
. My case, I want to make it simple.
If I click on the element and if it is anchor tag
or has page load kind of javascript
such as onclick=window.location.href='blah'
then I want to set the actionType to "clickAndWait"
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(6)
将元素对象转换为 mshtml.IHTMLDOMNode,然后 通过 IHTMLDOMNode.attributes 读取属性。 HtmlElement.GetAttribute 正在获取从嵌入属性生成的 jscript 函数的 IDispatch 接口。
根据盛江的回应,以下是一些工作示例:
IHTMLElement element = YourCodeToGetElement();
string onclick = string.Empty;
IHTMLDOMNode domNode = element as IHTMLDOMNode;
IHTMLAttributeCollection attrs = domNode.attributes;
foreach (IHTMLDOMAttribute attr in attrs)
{
if (attr.nodeName.Equals("onclick"))
{
string attrValue = attr.nodeValue as string;
if (!string.IsNullOrEmpty(attrValue))
{
onclick = attr.nodeValue;
break;
}
}
}
您可以尝试使用 HtmlAgilityPack 解析 webBrowser1.DocumentText
属性并然后使用 XPath 获得所需的结果。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有多种方法可以做到这一点。
另一种替代方法是使用自定义导航 URL 并对其进行操作
另一种替代方法是使用外部对象,WebBrowser 允许您设置 ObjectForScripting,您可以在 HTML 的 Javascript 中访问它。
.NET 2.0 中的 ObjectForScripting
您可以致电,
There are number of ways you can do it.
Another alternative is to use a custom navigation url and act upon it
Another Alternative method is to use External object, WebBrowser allows you to set an ObjectForScripting which you can access within Javascript of HTML.
ObjectForScripting in .NET 2.0
And you can call,