Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double click action not working whereas single click works on an element in selenium

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;">&nbsp;&nbsp;*&nbsp;Quarter&nbsp;to&nbsp;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();
like image 600
Ayisha Avatar asked Jan 27 '26 10:01

Ayisha


2 Answers

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"));
like image 117
IamBatman Avatar answered Jan 29 '26 00:01

IamBatman


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();
    

Update

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);
    
like image 33
undetected Selenium Avatar answered Jan 28 '26 23:01

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!