Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Browser emulation API

I am searching for .NET library/component which will allow me do following:

  1. Load web pages
  2. Execute linked JS
  3. Execute my own JS in page's scope and return result of executing, which can be complex object or array of objects.

The only solution I have found so far is using WebBrowser object, but it requires a good chunk of boilerplate code to make everything work and it fails to work with an array of objects. In the following code sample, I receive a RuntimeBinderException on dynamic 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();
    }
}

Can anyone provide an alternative solution or help to fix this one?

like image 731
Vasaka Avatar asked Jun 10 '26 04:06

Vasaka


1 Answers

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)

like image 57
Yahia Avatar answered Jun 15 '26 07:06

Yahia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!