3

There is a way to click on element by executing javascript like following:

((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

I want to double click on element by executing javascript, hence I tried like following:

((JavascriptExecutor)driver).executeScript("arguments[0].doubleClick();", element);

But it gives error:

org.openqa.selenium.WebDriverException: unknown error: undefined is not a function

Please tell me what I need to do in order perform double click by executing javascript.

Alpha
  • 13,320
  • 27
  • 96
  • 163

3 Answers3

6

You should use dblclick event

click, dblclick events

Mansuro
  • 4,558
  • 4
  • 36
  • 76
  • I receive the execption: arguments[0].dblclick is not a function – NTDLS Feb 09 '17 at 17:25
  • 1
    @NTDLS Can you give more details? – Mansuro Feb 09 '17 at 17:36
  • 1
    var row = browser.Driver.FindElement(By.ClassName("profile-row")); browser.JavaScriptExecutor.ExecuteScript("arguments[0].dblclick();", row); Exception: arguments[0].dblclick is not a function – NTDLS Feb 09 '17 at 17:43
  • check the following [github issue](https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1722) – Mansuro Feb 09 '17 at 17:52
2

Please try double click with mouse events:

((JavascriptExecutor) driver).executeScript("var evt = document.createEvent('MouseEvents');"+ 
    "evt.initMouseEvent('dblclick',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);"+ 
    "arguments[0].dispatchEvent(evt);", element);
slfan
  • 8,950
  • 115
  • 65
  • 78
  • This worked, thanks! See also [How do I double-click on objects using javascript?](https://stackoverflow.com/questions/23926921/how-do-i-double-click-on-objects-using-javascript-do-i-have-to-click-twice) – rdmolony May 03 '22 at 16:08
  • This worked flawlessly – Selenium Lover Aug 09 '23 at 13:53
1

This is the Java code for double click using JsExecutor

public void doubleClickWithJS(WebElement element) {
   JavascriptExecutor executor = (JavascriptExecutor) driver;
   executor.executeScript("arguments[0].dispatchEvent(new MouseEvent('dblclick', { bubbles: true }));", element);
}
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26