How to make mouse double click in JavaScript?

后端 未结 3 1066
说谎
说谎 2021-01-25 13:50

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 t

3条回答
  •  故里飘歌
    2021-01-25 14:21

    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));        " +
        "}                                                          " ;
      
    • Invoking the script through executeScript() from your @Test :

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

提交回复
热议问题