如何在JavaScript中双击鼠标? [英] How to make mouse double click in JavaScript?

查看:101
本文介绍了如何在JavaScript中双击鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个JavaScript代码来单独使鼠标双击.我将在Java代码中使用它.这是一个用于测试目的的硒项目,但是没有任何方法可以使鼠标双击硒,因此我想在我的Java代码中使用javaScript进行此操作.你有什么主意吗?

I need a JavaScript code to make mouse double click by itself. I will use it inside my Java code . This one is a selenium project for testing-purposes but there is not any way to make mouse double click in selenium so i want to use javaScript to do that inside my java code . Do you have any idea?

这是我的老问题如何双击网页上的任何地方?"

他们说我应该使用JavaScript双击鼠标,但是怎么做?

They said i should use JavaScript to make mouse double click but how ?

推荐答案

要创建 Mouse Double Click ,您可以编写脚本并将其传递给executeScript()方法,如下所示:

To make Mouse Double Click you can write a script and pass it to the executeScript() method as follows :

  • 脚本:

  • Script :

String jsDoubleClick = 
  "var target = arguments[0];                                 " +
  "var offsetX = arguments[1];                                " +
  "var offsetY = arguments[2];                                " + 
  "var rect = target.getBoundingClientRect();                 " +
  "var cx = rect.left + (offsetX || (rect.width / 2));        " +        
  "var cy = rect.top + (offsetY || (rect.height / 2));        " +
  "                                                           " +
  "emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
  "emit('mouseup',   {clientX: cx, clientY: cy});             " +
  "emit('mousedown', {clientX: cx, clientY: cy, buttons: 1}); " +
  "emit('mouseup',   {clientX: cx, clientY: cy});             " +
  "emit('click',     {clientX: cx, clientY: cy, detail: 2});  " +
  "                                                           " +
  "function emit(name, init) {                                " +
    "target.dispatchEvent(new MouseEvent(name, init));        " +
  "}                                                          " ;

  • 通过@Test@Test调用脚本:

  • Invoking the script through executeScript() from your @Test :

    new Actions(driver).moveToElement(myElem, posX, posY).perform();
    ((JavascriptExecutor)driver).executeScript(jsDoubleClick, myElem, posX, posY);
    

  • 这篇关于如何在JavaScript中双击鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆