1

我正在尝试调用模拟单击 WebBrowser 控件中的元素的 JS 脚本。

我目前正在这样做:

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

长模拟功能来自另一个 StackOverflow 答案。当我在 IE9 上的 web 开发工具控制台中键入它时,它工作正常,但这个脚本根本不起作用。我究竟做错了什么?

4

1 回答 1

1

[已编辑] 我在下面说的不正确。您的语法是正确的,webBrowser2.Document.InvokeScript("TestClick")应该与webBrowser2.InvokeScript("TestClick"). 我会试试你的代码然后回到这里。

我认为正确的语法应该是这样的:

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

()后注意TestClick

或者你可以这样做:

webBrowser2.InvokeScript("TestClick");

[已编辑] 最初加载到 WB 中的 HTML 有问题,也许您应该将其包含在您的问题中。以下只是您的代码的副本(除了this.webBrowser2.DocumentText),它确实有效,TestClick被调用。

    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");
    }
于 2013-08-14T03:10:53.840 回答