3

我正在搜索 .NET 库/组件,这将允许我执行以下操作:

  1. 加载网页
  2. 执行链接的 JS
  3. 在页面范围内执行我自己的JS并返回执行结果,可以是复杂对象或对象数组。

到目前为止,我发现的唯一解决方案是使用 WebBrowser 对象,但它需要大量样板代码才能使一切正常工作,并且无法处理对象数组。在以下代码示例中,我收到了 RuntimeBinderExceptiondynamic val = o.a;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var t = new System.Threading.Thread(BrowserThread);
        t.SetApartmentState(System.Threading.ApartmentState.STA);
        t.Start();

        Console.ReadLine();
    }

    private static void BrowserThread()
    {
        var wb = new WebBrowser();
        wb.Navigate("http://google.com");
        wb.DocumentCompleted += (sender, args) =>
            {
                var head = wb.Document.GetElementsByTagName("head")[0];
                var script = wb.Document.CreateElement("script");
                dynamic el = script.DomElement;
                el.text = "function test(){return [{a:2},{a:2},{a:2}]}";
                head.AppendChild(script);
                dynamic result = wb.Document.InvokeScript("test");
                dynamic o = result.pop();
                dynamic val = o.a;
            };
        Application.Run();
    }
}

任何人都可以提供替代解决方案或帮助解决这个问题吗?

4

1 回答 1

2

you could try dynamic a = o[0].a; or dynamic a = result[0].a; and see if that helps...

Another point: you should check the type of result by stepping though your code... this might help finding a working answer...

Regarding alternatives there are some options:

  • WebKit.Net (free)

  • Awesomium
    It is based on Chrome/WebKit and works like a charm. There is a free license available but also a commercial one and if need be you can buy the source code :-)

  • WatiN (also free)

于 2012-12-18T22:09:26.063 回答