如何从WebBrowser控件的onclick得到实际的JavaScript值? [英] How to get actual JavaScript value in onclick from webbrowser control?

查看:1126
本文介绍了如何从WebBrowser控件的onclick得到实际的JavaScript值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方式来获得的onclick中定义JavaScript的code。
我使用.NET 2.0 C#的Visual Studio 2005。

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.

示例:

<span id="foo" onclick+"window.location.href='someURL'>click here</span>

我的目标是让字符串window.location.href ='someURL'

情景:

一个用户点击一个网页元素,例如上面显示,WebBrowser控件内的标签上。然后点击标签吹罚于H tmlElement对象

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.

在WebBrowser控件我然后调用的HtmlElement对象的getAttribute(onclick事件),它只是给我的系统.__ ComObject。

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

输出:

dom elem  >>>>>>>>>>> mshtml.HTMLSpanElementClass
js value ===> System.__ComObject

由于它表明,span.onclick还是给我系统.__ ComObject,我究竟做错了什么?

As it shown, span.onclick still give me System.__ComObject, what am I doing wrong?

Why确实的HtmlElement的的getAttribute()方法返回mshtml.HTMLInputElementClass,而不是属性的值? 的这家伙说,它的工作在他的情况,我已经跟着它,但我是有点不工作..

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...

更新

研究,研究.....

Research, research.....

我可以添加引用VisualBasic.dll我的C#项目,然后调用该方法,找出谁是这个系统.__ ComObject确实是。

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

输出:

JScriptTypeInfo

看起来这是一个JScript型...我怎样才能访问该对象?

It looks like this is a JScript type... how can I access this object?

的更多细节

上面的描述是基于我当前的项目。该项目是建立像硒IDE。它使用WebBrowser控件来代替。

The above description is based on my current project. The project is to create something like Selenium IDE. It uses WebBrowser control instead.

硒IDE将创建3种不同的东西来记录Web文档中的一个元素。

Selenium IDE creates 3 different things to record an element in the web document.

1. actionType
2. xpath
3. value

例如,

type, //input[@id=foo], "hello world"
clickAndWait, //link=login, ""

硒IDE识别页面加载,因此之间改变操作类型单击clickAndWait。我的情况,我想让它简单。

Selenium IDE recognize page load so it changes actionType between "click" and "clickAndWait". My case, I want to make it simple.

如果我点击元素,如果它是锚标记或具有页面加载样的JavaScript
的onclick = window.location.href ='嗒嗒'那么我想设置的操作类型为clickAndWait

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".

推荐答案

有办法号码,你可以做到这一点。

There are number of ways you can do it.


  1. 有一个在DOM事件对象,这将给你哪些元素生成此事件的信息。

  2. 您可以在这里看看,<一个href=\"http://msdn.microsoft.com/en-us/library/ff975965%28v=VS.85%29.aspx\">http://msdn.microsoft.com/en-us/library/ff975965%28v=VS.85%29.aspx

  3. 这个人是不错的,你可以很容易利用这一点,你会得到事件对象作为参数的方法,你可以调查的参数,找出事件的根源。 http://support.microsoft.com/kb/312777

  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. 覆盖BeforeNavigate事件

  2. 检查导航网址是否包含mycommand:点击或mycommand:clickandwait3.如果它包含任何的这个,然后设置取消为真。 (这将停止浏览器导航)。

  3. 然后你可以从你的C#code浏览您的网页浏览code,并通过取消为真。

另一种替代方法是使用外部对象,web浏览器允许您设置一个<一个href=\"http://msdn.microsoft.com/en-us/library/SYSTEM.WINDOWS.CONTROLS.WEBBROWSER.OBJECTFORSCRIPTING%28v=vs.100,d=lightweight%29.aspx\">ObjectForScripting您可以在HTML中的JavaScript访问。

Another Alternative method is to use External object, WebBrowser allows you to set an ObjectForScripting which you can access within Javascript of HTML.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting%28v=VS.80%29.aspx\">ObjectForScripting在.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

和可以打电话,

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

这篇关于如何从WebBrowser控件的onclick得到实际的JavaScript值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆