如何在JavaScript中双击鼠标?

时间:2017-12-26 16:15:47

标签: javascript selenium selenium-webdriver

我需要一个JavaScript代码才能让鼠标双击。我将在我的Java代码中使用它。这是一个用于测试目的的selenium项目,但是没有任何方法可以在selenium中进行鼠标双击,所以我想在我的java代码中使用javaScript来实现。你有什么主意吗?

This is old question of me "How to double click any where on web page?"

他们说我应该使用JavaScript来鼠标双击,但是如何?

3 个答案:

答案 0 :(得分:0)

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 !
});

我经常在数据表中使用它:如果用户选择"打开模态"我有一个弹出框(每行左边的一个小菜单)。然后我触发双击,进入已经等待行上用户双击的表格。所以我不需要实施2个活动

答案 1 :(得分:0)

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

  • 脚本:

    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));        " +
      "}                                                          " ;
    
  • 通过executeScript()

    中的@Test调用脚本
    new Actions(driver).moveToElement(myElem, posX, posY).perform();
    ((JavascriptExecutor)driver).executeScript(jsDoubleClick, myElem, posX, posY);
    

答案 2 :(得分:-1)

使用JQuery:

 $(selector).dblclick()