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

隐身守侯 提交于 2019-12-05 04:03:00

问题


I have a canvas element on my page and I want to click on specific (x, y) coordinates in this canvas. I use watir-webdriver:

element = browser.driver.find_element(:id, 'canvas')
browser.driver.action.move_to(element).move_by(x, y).click().perform

But this code just clicks on the center of the canvas, not the specified (x, y) coordinates. What is wrong with it?

UPD: So now I use this code:

element = browser.driver.find_element(:id, 'canvas')
browser.driver.action.move_to(element, x, y).perform
browser.driver.click.perform

But it still clicks on the center of the canvas and not on specified (x, y) coordinates... Any thoughts?

UPD 2: This is only the FIREFOX issue (works well in Chrome)


回答1:


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.




回答2:


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!




回答3:


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




回答4:


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

            robot.mouseMove(x, y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);


来源:https://stackoverflow.com/questions/20285254/how-to-click-on-specific-element-in-canvas-by-its-coordinates-using-webdriver

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