How to make mouse double click in JavaScript?

后端 未结 3 1052
说谎
说谎 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);
      
    0 讨论(0)
  • 2021-01-25 14:26

    Using JQuery:

     $(selector).dblclick()
    
    0 讨论(0)
  • 2021-01-25 14:33
    As Mozfet says JQuery ! it's so easy with JQuery ! 
    
    $("#myId").trigger('dblclick');
    
    then you listen for this double click 
    $("#myId").on('dblclick',function(){
    // do it !
    });
    
    // you can event make you own event 
    $("#myId").trigger('retrieve');
    
    then you listen for this custom event 
    $("#myId").on('retrieve',function(){
    // do it !
    });
    

    I often use it in datatables : I've got a popover (a small menu on the left td of each row) if the user choose "open modal" i then trigger a double click which goes to the table which is already waiting for a double click from the user on the row. So i don't need to implement 2 events

    0 讨论(0)
提交回复
热议问题