1

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?

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

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

Anoretu
  • 65
  • 6
  • 1
    If you look in the docs, it states what the available option is... Use [Actions.doubleClick(WebElement)](http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html#doubleClick-org.openqa.selenium.WebElement-). No need for javascript workarounds. – JeffC Dec 26 '17 at 17:09
  • 1
    I don't want to click a normal web element. I will click on a Google Map so it doesn't work. Click() method works but DoubleClick() is useless in my case. – Anoretu Dec 27 '17 at 05:10
  • A Google map **IS** a normal element. It's made up of a `CANVAS` tag. What do you think you are double-clicking with JS? – JeffC Dec 27 '17 at 06:21
  • @JeffC OP was looking for an Answer through JavaScript. I fail to understand what's wrong with JavaScript which is one of the most widely accepted and powerful `Selenium` supported binding as well as integrated within `Selenium-Java` and `Selenium-Python` clients. Your unawareness of JavaScript and subsequent downvotes on potential good answers are turning out to be counter productive for stackoverflow community. – DebanjanB Dec 27 '17 at 06:35
  • @DebanjanB OP is looking for a Javascript answer because you told him to in his previous question (which wasn't necessary). i am fully aware of JS and what it's capabilities are but at some point you need to ask yourself... why am I using Selenium if all I'm going to use is it's ability to run JS commands? Using JS is not a user scenario which is what many people are trying to accomplish when writing automation. By telling inexperienced people to use JS without explaining other better options and what the downsides are, YOU are being counterproductive. – JeffC Dec 27 '17 at 15:55

3 Answers3

1

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);
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
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 !
});

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

PhilMaGeo
  • 533
  • 6
  • 7
  • Thanks but problem is i can't call it from a Java Code . – Anoretu Dec 26 '17 at 16:55
  • I thought it was a typo ! why do you ever want to call it from the back ? common ways are write some js from the back at parsing time so the code is executed at loading time in the browser. Or you ask something to the back with an ajax call and then with a particular response you trigger your event on the browser. What do you want exactly ? don't hesitate to explain a lot more – PhilMaGeo Dec 26 '17 at 17:03
-1

Using JQuery:

 $(selector).dblclick()
Mozfet
  • 319
  • 2
  • 11