Steve Staple
Steve Staple

Reputation: 3279

Selenium Java click performed on element did not work

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.

Upvotes: 0

Views: 106

Answers (2)

Amit Jain
Amit Jain

Reputation: 4587

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

Upvotes: 1

Ishita Shah
Ishita Shah

Reputation: 4035

Please check with JavaScriptExecutor:

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

Upvotes: 1

Related Questions