I'm trying to write the code to click a canvas center in the web page.
Here is the code below:
private static void clickCanvasCenter() {
WebElement we = driver.findElement(By.tagName("canvas"));
int x = we.getSize().width/2;
int y = we.getSize().height/2;
Actions builder = new Actions(driver).moveToElement(new WebDriverWait(driver,20)
.until(ExpectedConditions.elementToBeClickable(we)));
System.out.println("width:" + x + "\theight:" + y);
builder.click().build().perform();
System.out.println("clicked:1");
builder.moveByOffset(x, y).click().build().perform();
System.out.println("clicked:2");
}
and the output is:
width:683 height:341
clicked:1
Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: move target out of bounds
(Session info: chrome=79.0.3945.130)
In above code that if I did not move the mouse by moveByOffset() method, the click action can be executed (because you can see 'clicked:1'), however it did not click the center of the canvas element. If I tried to move the mouse across the canvas element, then the exception raised.
How can I make it work and click the center of the canvas element?