0

The following appears in my test automation code. It reports that is has worked, but it didn't. Can I break this down & find out why?

Actions actions = new Actions(driver);

actions.moveToElement(element).click().build().perform();

I have already found the element, tested that it is displayed & clickable at this point, & wrapped the whole lot in a try/catch to check for errors (no errors reported).

I think the problem is that the 'element.isDisplayed' function gives misleading results.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Steve Staple
  • 2,983
  • 9
  • 38
  • 73
  • 2
    (I think you can skip "build()") Put a print statement right before and right after the command to click the element and see if both are displayed. – Alichino Dec 11 '18 at 12:11
  • yes we should skip `build()` – Amit Jain Dec 11 '18 at 12:43
  • Are you sure the element found by the selector is the one you want to click? If yes, could you try to click it using javascript ? – Mr Cas Dec 11 '18 at 15:53
  • What version of Selenium? Are you running this through RemoteWebDriver (maybe like SauceLabs or BrowserStack)? – SiKing Dec 12 '18 at 19:23

2 Answers2

1

Way 1 - Try to click directly when you have WebElement

WebElement one = driver.findElement(By.name("one"));
WebElement two = driver.findElement(By.name("two"));

Actions actions = new Actions(driver);
actions.click(one)
.click(two)
.build().perform();

Way 2 - Try to skip build() and it can be used with single/double click

WebElement sngClick= driver.findElement(By.name("sngClick"));
WebElement dblClick= driver.findElement(By.name("dblClick"));

Actions actions = new Actions(driver);
actions.moveToElement(sngClick).click().perform();
actions.moveToElement(dblClick).doubleClick().perform();
Amit Jain
  • 4,389
  • 2
  • 18
  • 21
  • Amit Jain - neither of those options make sense. Why would you do click(one) twice? Also, defining WebElement one twice would be illegal. – Steve Staple Dec 11 '18 at 13:40
  • @SteveStaple - I have updated the answer, I wrongly mentioned one element name 2 times. Actually there are 2 webelements and we want to click them one by one in a chain of actions, then we can use this solution. In second solution we can move to element and then click it, here build() is not required, this looks more appropriate to your question. You can also try javascript solution as mentioned in another solution. – Amit Jain Dec 12 '18 at 06:50
1

Please check with JavaScriptExecutor:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(WebElement));
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51