简体   繁体   English

无法使用Selenium WebDriver在画布上找到元素

[英]Unable to locate the Element on Canvas using Selenium WebDriver

I have an application developed by using Vaadin Framework, Now i need to click on the rectangular polygon which is on the Canvas.following is the html code here i am providing the Html code 我有一个使用Vaadin Framework开发的应用程序,现在我需要单击Canvas上的矩形多边形。以下是这里的html代码,我正在提供HTML代码

<canvas width="1920" height="524" class="ol-unselectable" style="width: 100%; height: 100%;"></canvas>

and i tried by using Actions which makes the mouse move over the Polygon and click . 我尝试使用动作使鼠标移至“多边形”上并单击。

int x = (int) 5638326.333511386;
int y = (int)  2580101.9711508946;
driver.get("http://localhost:8080/internship");

WebElement ele = driver.findElement(By.xpath("//canvas[@class='ol-unselectable']"));
        //  driver.findElement(By.tagName("canvas"));
        //driver.findElemet(By.className("ol-unselectable"));
try {
    Actions builder = new Actions(driver);
    builder.moveToElement(ele, x, y);
    builder.clickAndHold();
    builder.release();
    builder.perform();
    } catch (Exception e) {
        // do nothing
    }

i am getting the foloowing error 我收到以下错误

org.openqa.selenium.NoSuchElementException: Unable to locate element: //canvas[@class='ol-unselectable']. org.openqa.selenium.NoSuchElementException:无法找到元素:// canvas [@ class ='ol-unselectable']。

can anyone suggest some samples how to find polygon on canvas with co-ordinates and make click on it. 谁能提出一些示例,说明如何使用坐标在画布上查找多边形并单击它。

Usually, the canvas element is embedded in an iframe. 通常,canvas元素嵌入在iframe中。 So, first, you have to find the iframe element and then find the canvas inside the iframe. 因此,首先,您必须找到iframe元素,然后在iframe中找到画布。 For instance: 例如:

WebDriver driver = new FirefoxDriver(firefoxOptions);
        try {
            driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_canvas_empty");
            WebElement iframe = driver.findElement(By.name("iframeResult"));
            driver.switchTo().frame(iframe);
            WebElement canvas = driver.findElement(By.id("myCanvas"));
            System.out.println(canvas.getText());
        } finally {
            driver.quit();
        }

I think this code might help you. 我认为这段代码可能会对您有所帮助。

EDIT: After chatting with @RamanaMuttana and his changes on the posted question, I could better understand his need. 编辑:在与@RamanaMuttana聊天以及他对发布的问题进行更改后,我可以更好地了解他的需求。

We realized that just using the By.tagName selector was enough to find the canvas element as in the code bellow: 我们意识到,只需使用By.tagName选择器就足以找到canvas元素,如下面的代码所示:

driver.findElements(By.tagName("canvas")).get(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM