Home > database >  Selenium - Java - Actions Class - Click not working
Selenium - Java - Actions Class - Click not working

Time:12-14

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  data-dojo-attach-point="_r2Cont">
   <div  data-dojo-attach-point="_btnsCont1">
      <div  title="preview" data-dojo-attach-point="_previewBtn">
         <svg >
            <use  xlink:href="#zi-eye-inactive"></use>
         </svg>
      </div>
      <div ></div>
      <div  title="code" data-dojo-attach-point="_codeBtn">
         <svg >
            <use  xlink:href="#zi-code"></use>
         </svg>
      </div>
   </div>
   <div  data-dojo-attach-point="_btnsCont2">
      <div  data-dojo-attach-point="_previewInTabBtn">
         <svg >
            <use  xlink:href="#zi-launch"></use>
         </svg>
         <div>Preview in tab</div>
      </div>
      <div  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

CodePudding user response:

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.

CodePudding user response:

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