如何从浏览器控件获取 onclick 中的实际 JavaScript 值?

发布于 11-14 04:43 字数 2359 浏览 7 评论 0原文

我正在寻找一种方法来获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

故人如初2024-11-21 04:43:32

有多种方法可以做到这一点。

  1. DOM 中有一个 Event 对象,它将为您提供有关哪个元素生成此事件的信息。
  2. 您可以在这里查看 http://msdn.microsoft.com/ en-us/library/ff975965%28v=VS.85%29.aspx
  3. 这个很好,你可以很容易地使用它,你会得到事件对象作为方法参数,你可以调查参数来找出事件对象事件的来源。 http://support.microsoft.com/kb/312777

另一种替代方法是使用自定义导航 URL 并对其进行操作

  1. 覆盖 BeforeNavigate 事件
  2. 检查导航 url 是否包含“mycommand:click”或“mycommand:clickandwait” 3. 如果包含其中任何一项,则将 cancel 设置为 true。 (这将停止浏览器导航)。
  3. 然后,您可以从 C# 代码导航您的网络浏览器代码,并将 cancel 传递为 true。

另一种替代方法是使用外部对象,WebBrowser 允许您设置 ObjectForScripting,您可以在 HTML 的 Javascript 中访问它。

.NET 2.0 中的 ObjectForScripting

[ComVisible(true)]
public class MyClass
{
   // can be called from JavaScript
   public void ShowMessageBox(string msg){
       MessageBox.Show(msg);
   }
}

myBrowser.ObjectForScripting = new MyClass(); 
// or you can reuse instance of MyClass

您可以致电,

window.external.ShowMessageBox("This was called from JavaScript");

There are number of ways you can do it.

  1. There is an Event object in DOM, which will give you information about which element generated this event.
  2. You can look at here, http://msdn.microsoft.com/en-us/library/ff975965%28v=VS.85%29.aspx
  3. This one is good, you can use this easily, you will get the event object as method parameter which you can investigate parameters to find out the source of the event. http://support.microsoft.com/kb/312777

Another alternative is to use a custom navigation url and act upon it

  1. Override BeforeNavigate event
  2. Check for Navigation url if it contains "mycommand:click" or "mycommand:clickandwait" 3. If it contains any of this, then set cancel as true. (this will stop navigation by browser).
  3. Then you can navigate your webbrowser code from your C# code and pass cancel as true.

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

[ComVisible(true)]
public class MyClass
{
   // can be called from JavaScript
   public void ShowMessageBox(string msg){
       MessageBox.Show(msg);
   }
}

myBrowser.ObjectForScripting = new MyClass(); 
// or you can reuse instance of MyClass

And you can call,

window.external.ShowMessageBox("This was called from JavaScript");
趁微风不噪2024-11-21 04:43:32

将元素对象转换为 mshtml.IHTMLDOMNode,然后 通过 IHTMLDOMNode.attributes 读取属性。 HtmlElement.GetAttribute 正在获取从嵌入属性生成的 jscript 函数的 IDispatch 接口。

Cast the element object to mshtml.IHTMLDOMNode, then read the attributes via IHTMLDOMNode.attributes. HtmlElement.GetAttribute is getting the IDispatch interface of the jscript function generated from the embedded attribute.

过度放纵2024-11-21 04:43:32

根据盛江的回应,以下是一些工作示例:

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

As per Sheng Jiang's response, here is some working sample:

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;
        }
    }
}
筑梦2024-11-21 04:43:32

您可以尝试使用 HtmlAgilityPack 解析 webBrowser1.DocumentText 属性并然后使用 XPath 获得所需的结果。

You can try to parse webBrowser1.DocumentText property using HtmlAgilityPack and then get desired result using XPath.

嘿嘿嘿2024-11-21 04:43:32

如果您不必使用 C# 来完成此操作(您可以使用 JS 来完成并创建回发),您应该查看 这个问题。

If you don't HAVE to do it with C# (you can do it with JS and create a Postback) you should take a look at THIS question.

七七2024-11-21 04:43:32

您可以通过首先阅读 obj.outerHtml 轻松地自行解析它。这应该会为您提供该 obj 的整个 html,然后在其中搜索值 onclick="????"并提取???部分。

You can parse it yourself easily, by first reading obj.outerHtml. That should give you the entire html for that obj, then search it for the value onclick="????" and extract the ???? part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文