1

I'm trying to invoke a JS script that simulates a click on an element in the WebBrowser Control.

I'm currently doing this:

webBrowser2.Invoke(new Action(() =>
                {
                    HtmlElement head = webBrowser2.Document.GetElementsByTagName("head")[0];
                    HtmlElement testScript = webBrowser2.Document.CreateElement("script");
                    IHTMLScriptElement element = (IHTMLScriptElement)testScript.DomElement;
                    element.text = "function simulate(g,c){var e=extend(defaultOptions,arguments[2]||{});var b,f=null;for(var d in eventMatchers){if(eventMatchers[d].test(c)){f=d;break}}if(!f){throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported')}if(document.createEvent){b=document.createEvent(f);if(f=='HTMLEvents'){b.initEvent(c,e.bubbles,e.cancelable)}else{b.initMouseEvent(c,e.bubbles,e.cancelable,document.defaultView,e.button,e.pointerX,e.pointerY,e.pointerX,e.pointerY,e.ctrlKey,e.altKey,e.shiftKey,e.metaKey,e.button,g)}g.dispatchEvent(b)}else{e.clientX=e.pointerX;e.clientY=e.pointerY;var a=document.createEventObject();b=extend(a,e);g.fireEvent('on'+c,b)}return g}function extend(a,c){for(var b in c){a[b]=c[b]}return a}var eventMatchers={HTMLEvents:/^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,MouseEvents:/^(?:click|dblclick|mouse(?:down|up|over|move|out))$/};var defaultOptions={pointerX:0,pointerY:0,button:0,ctrlKey:false,altKey:false,shiftKey:false,metaKey:false,bubbles:true,cancelable:true};function TestClick(){simulate(document.getElementById('flls').firstChild,'click')};";
                    head.AppendChild(testScript);
                    webBrowser2.Document.InvokeScript("TestClick");

                    string TestUrl = webBrowser2.Url.AbsoluteUri;
                    if (TestUrl.Equals(expected))
                    {
                        HasSucceeded = 1;
                    }
                    else
                    {
                        // No test
                    }
                }));

The long simulate function is from another StackOverflow answer. When I type this in the web dev tools console on IE9 it works fine, but this script doesn't work at all. What am I doing wrong?

1 Answer 1

1

[EDITED] What I said below was incorrect. Your syntax is correct, webBrowser2.Document.InvokeScript("TestClick") should do the same job as webBrowser2.InvokeScript("TestClick"). I'll try your code and get back here.

I think the correct syntax should be this:

webBrowser2.Document.InvokeScript("TestClick()");

Note () after TestClick.

Or you could rather do:

webBrowser2.InvokeScript("TestClick");

[EDITED] There's something wrong with the HTML originally loaded into the WB, maybe you should include it with your question. The following is just a copy of your code (besides this.webBrowser2.DocumentText) and it does work, TestClick gets invoked.

    private void Form1_Load(object sender, EventArgs e)
    {
        this.webBrowser2.DocumentText = "<body><div id=flls><button onclick='alert(true)'>go</button></div></body>";
        this.webBrowser2.DocumentCompleted += webBrowser2_DocumentCompleted;
    }

    void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        HtmlElement head = webBrowser2.Document.GetElementsByTagName("head")[0];
        HtmlElement testScript = webBrowser2.Document.CreateElement("script");
        IHTMLScriptElement element = (IHTMLScriptElement)testScript.DomElement;
        element.text = "function simulate(g,c){var e=extend(defaultOptions,arguments[2]||{});var b,f=null;for(var d in eventMatchers){if(eventMatchers[d].test(c)){f=d;break}}if(!f){throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported')}if(document.createEvent){b=document.createEvent(f);if(f=='HTMLEvents'){b.initEvent(c,e.bubbles,e.cancelable)}else{b.initMouseEvent(c,e.bubbles,e.cancelable,document.defaultView,e.button,e.pointerX,e.pointerY,e.pointerX,e.pointerY,e.ctrlKey,e.altKey,e.shiftKey,e.metaKey,e.button,g)}g.dispatchEvent(b)}else{e.clientX=e.pointerX;e.clientY=e.pointerY;var a=document.createEventObject();b=extend(a,e);g.fireEvent('on'+c,b)}return g}function extend(a,c){for(var b in c){a[b]=c[b]}return a}var eventMatchers={HTMLEvents:/^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,MouseEvents:/^(?:click|dblclick|mouse(?:down|up|over|move|out))$/};var defaultOptions={pointerX:0,pointerY:0,button:0,ctrlKey:false,altKey:false,shiftKey:false,metaKey:false,bubbles:true,cancelable:true};function TestClick(){simulate(document.getElementById('flls').firstChild,'click')};";
        head.AppendChild(testScript);
        webBrowser2.Document.InvokeScript("TestClick");
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.