Selenium Java click performed on element did not work

101 views Asked by At

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.

2

There are 2 answers

2
Amit Jain On

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();
0
Ishita Shah On

Please check with JavaScriptExecutor:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(WebElement));