在Web浏览器控件中调用Javascript Test Click

本文关键字:Javascript Test Click 调用 Web 浏览器 控件 | 更新日期: 2023-09-27 18:27:29

我正在尝试调用一个JS脚本,该脚本模拟点击WebBrowser控件中的元素。

我目前正在做这个:

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开发工具控制台中键入此脚本时,它运行良好,但此脚本根本不起作用。我做错了什么?

在Web浏览器控件中调用Javascript Test Click

[编辑]我下面说的不正确。你的语法是正确的,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");
    }