简体   繁体   中英

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

<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'].

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. So, first, you have to find the iframe element and then find the canvas inside the 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.

We realized that just using the By.tagName selector was enough to find the canvas element as in the code bellow:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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