Selenium - Java - Actions Class - Click not working

412 views Asked by At

When I use the following code for click or double click using Actions class, it seems like mouse click triggers but is not released. Apparently, the button is highlighted, but click doesn't perform completely.

Click Code:

WebElement refreshPreviewButton = driver.findElement(By.xpath("//div[@data-dojo-attach-point='_previewInTabBtn']"));
Actions action = new Actions(driver);
// Approach 1
action.click(refreshPreviewButton).build().perform();

// Approach 2
action.movetoElement(refreshPreviewButton).click().build().perform();

Double Click Code:

WebElement refreshPreviewButton = driver.findElement(By.xpath("//div[@data-dojo-attach-point='_previewInTabBtn']"));
Actions action = new Actions(driver);

// Approach 1
action.doubleClick(refreshPreviewButton).build().perform();

// Approach 2
action.movetoElement(refreshPreviewButton).doubleClick().build().perform();

Working code of other element on the same page:

WebElement previewInTabButton = driver.findElement(By.xpath("//div[@data-dojo-attach-point='_previewInTabBtn']"));
Actions action = new Actions(driver);
action.doubleClick(previewInTabButton);
action.perform();

HTML:

<div class="c3_axnPrvCHR2 c3_rows_nowrap c3_align_center c3_hidden" data-dojo-attach-point="_r2Cont">
   <div class="c3_axnPrvCBtns c3_axnPrvCBtnsL c3_rows_nowrap c3_align_center btn-sgmt-grp btn-group c3_br4 c3_bdr" data-dojo-attach-point="_btnsCont1">
      <div class="btn-sgmt c3_btn btn-icn-sq btn-icn-sq-34" title="preview" data-dojo-attach-point="_previewBtn">
         <svg class="c3i c3i_txt">
            <use class="c3i_u" xlink:href="#zi-eye-inactive"></use>
         </svg>
      </div>
      <div class="c3_bdr_r"></div>
      <div class="btn-sgmt c3_btn btn-icn-sq btn-icn-sq-34" title="code" data-dojo-attach-point="_codeBtn">
         <svg class="c3i c3i_txt">
            <use class="c3i_u" xlink:href="#zi-code"></use>
         </svg>
      </div>
   </div>
   <div class="c3_axnPrvCBtns c3_axnPrvCBtnsR c3_rows_nowrap c3_align_center c3_gpC16" data-dojo-attach-point="_btnsCont2">
      <div class="btn-default-outline2_new btn-sm c3_btn btn-fx" data-dojo-attach-point="_previewInTabBtn">
         <svg class="c3i c3i_txt">
            <use class="c3i_u" xlink:href="#zi-launch"></use>
         </svg>
         <div>Preview in tab</div>
      </div>
      <div class="btn-default-outline2_new btn-sm c3_btn btn-fx" data-dojo-attach-point="_refreshPreviewBtn">
         <div>Preview</div>
      </div>
   </div>
</div>

Edit 1: Added HTML

Edit 2: Added working code of other element on same page

2

There are 2 answers

0
Prophet On BEST ANSWER

In case clicking web element with Actions click or double click seems like the element was clicked / touched but the click / double click did not actually performed we can add a sleep like Thread.sleep(2000); before applying the Actions click.
Waiting for element clickability or visibility may not be enough here since Actions click works not exactly as Selenium click works. So Actions click may need the element to be in the visible viewport and clickable and also the page should be is a stable state, not during scrolling or rendering process.
So, generally I'd suggest to apply here to actions: wait for element clickability + additional sleep, like the following:

WebElement useContentButton = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@data-dojo-attach-point='_previewInTabBtn']")));
Thread.sleep(2000);
Actions action = new Actions(driver);
action.doubleClick(useContentButton).build().perform();
0
Your Storyteller On

For anyone who is having a similar issue:

I had to add wait before performing action.

Thread.sleep(2000);
Actions action = new Actions(driver);
action.doubleClick(useContentButton).build().perform();

I got this trick from hover over each stars and then finally click on 4th star.