Java – Double-click action does not work, while single-click action does for elements in selenium

Double-click action does not work, while single-click action does for elements in selenium… here is a solution to the problem.

Double-click action does not work, while single-click action does for elements in selenium

I want to double-click an element but can’t do it. Clicking on the same element works well. Am I missing something? Can someone help me with this.

The HTML: element

<tbody><tr class="mclS" tabindex="0"> <td><div class="mclC" style="height:14px; ">&nbsp; &nbsp; *&nbsp; Quarter&nbsp; to&nbsp;Date</div></td> </tr> </tbody>

I tried multiple ways to double-click an element :

WebElement date = driver.findElement(By.cssSelector(".mlstBody>tbody>tr:nth-child(8)"));

=> actions.doubleClick(date).build().perform();

=> actions.doubleClick(date);

=> ((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);" ,date);

=> actions.moveToElement(date).doubleClick().build();
actions.perform();

Solution

I guess you’re using Firefox? I think there is a problem with doubleclick and geckodriver. I don’t think it’s finalized yet. I see you tried one approach in JavaScript. Can you try this way? It worked for me in Firefox.

document.querySelector(".mlstBody>tbody>tr:nth-child(8)").dispatchEvent(new MouseEvent("dblclick"));

Related Problems and Solutions