I am trying to double click on an element but unable to perform the action. Single click works fine on the same element. Am i missing something?. Can someone please help me out with this.
HTML of the element:
<tbody><tr class="mclS" tabindex="0"> <td><div class="mclC" style="height:14px;"> * Quarter to Date</div></td> </tr> </tbody>
I have tried various ways of double clicking the 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();
I'm going to guess you are using Firefox? I think there is an issue written up with doubleclick and the geckodriver. I don't think it's fixed yet. I see you tried one way in JavaScript. Can you try this way though? It worked for me in Firefox.
document.querySelector(".mlstBody>tbody>tr:nth-child(8)").dispatchEvent(new MouseEvent("dblclick"));
Seems you were pretty close. To invoke doubleClick() through Actions class you can use either of the following solutions:
Using cssSelector:
WebElement date = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("tr.mclS>td>div.mclC")));
new Actions(driver).moveToElement(date).doubleClick().build().perform();
Using xpath:
WebElement date = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//tr[@class='mclS']/td/div[@class='mclC' and contains(.,'Date')]")));
new Actions(driver).moveToElement(date).doubleClick().build().perform();
As you are still unable to invoke doubleClick() on the desired element as an alternative 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With