1

我如何在网络浏览器上调用鼠标右键/中键单击

我正在尝试使用javascript在网络浏览器中模拟右键单击链接,并使用此代码进行模拟右键单击,但都不起作用!

HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("A");

foreach (HtmlElement link in links) 
        object[] args = { link };
        HtmlElement script1 = webBrowser1.Document.CreateElement("script");
        HtmlElement script2 = webBrowser1.Document.CreateElement("script");
        HtmlElement script3 = webBrowser1.Document.CreateElement("script");
        HtmlElement script4 = webBrowser1.Document.CreateElement("script");
        script1.SetAttribute("rightc1", "function rightc1(thiselements)
         {var element = thiselements;var e = element.ownerDocument.createEvent('MouseEvents');
           e.initMouseEvent('contextmenu', true, true,element.ownerDocument.defaultView, 
                                       1, 0, 0, 0, 0, false,false, false, false,2, null);
           return !element.dispatchEvent(e);}");
        script2.SetAttribute("rightc2", "function rightc2(thiselements){$('#thiselements').trigger({type: 'mousedown',which: 3});}");
        script3.SetAttribute("rightc3", "function rightc3(thiselements){$('#thiselements').trigger({type: 'mouseup',which: 3});}");
        script4.SetAttribute("rightc4", "function rightc4(thiselements)
                              {$('#thiselements').trigger({type: 'mousedown',which: 3}).
                                      trigger({type: 'mouseup',which: 3});}");
        link.AppendChild(script1);
        webBrowser1.Document.InvokeScript("rightc1", args);    

        link.AppendChild(script2);
        webBrowser1.Document.InvokeScript("rightc2", args);        

        link.AppendChild(script3);
        webBrowser1.Document.InvokeScript("rightc3", args);

        link.AppendChild(script4);
        webBrowser1.Document.InvokeScript("rightc4", args);

我也知道我可以用这些代码模拟左键单击,link.InvokeMember("Click"); 但我想右键单击,但我不知道如何模拟它。

我也不想移动鼠标并模拟点击。

4

0 回答 0