How to click on specific element in canvas by its coordinates (using WebDriver)?

无人久伴 提交于 2019-12-03 21:43:36

Movement

move_to(element) moves to the center of the element specified and move_by is a relative move. So by the end of these two operations, you have moved to the coordinates (x of element center + x, y of element center + y).

You should use move_to(element, x, y). This will move to the x, y coordinates relative to the origin of the element.

Relevant documentation.

Firefox

Are you using a version of Selenium and Firefox for which Selenium supports native events? The combination of Selenium 2.37 with Firefox 24 does. I've had test suite fails just because native events were not available.

I got Selenium to select an area within the Canvas element using the following method:

public void selectCanvasArea(int xCanvas, int yCanvas, int xTarget, int yTarget) {
    action.moveToElement(driver.findElement(By.id("Canvas")),xCanvas,yCanvas) //(300,300)
            .clickAndHold()
            .moveByOffset(xTarget,yTarget) //(600,150)
            .release()
            .perform();

Good luck!

Download latest selenium webdriver 2.42.1.Tested and it's working in firefox

            Robot robot = new Robot();
            robot.delay(3000);

            robot.mouseMove(x, y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!