无法使用 Selenium WebDriver 在画布上定位元素

罗摩那木他那

我有一个使用 Vaadin 框架开发的应用程序,现在我需要点击画布上的矩形多边形。以下是 html 代码,我提供了 Html 代码

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

我尝试使用 Actions 使鼠标移动到 Polygon 上并单击 。

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
    }

我收到以下错误

org.openqa.selenium.NoSuchElementException:无法定位元素://canvas[@class='ol-unselectable']。

任何人都可以建议一些示例如何使用坐标在画布上找到多边形并单击它。

蒂亚戈普罗塞姆

通常,canvas 元素嵌入在 iframe 中。因此,首先,您必须找到 iframe 元素,然后在 iframe 中找到画布。例如:

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();
        }

我认为此代码可能对您有所帮助。

编辑:在与@RamanaMuttana 聊天以及他对已发布问题的更改后,我可以更好地理解他的需求。

我们意识到仅使用 By.tagName 选择器就足以找到画布元素,如下面的代码所示:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在 python 上使用 selenium webdriver 定位元素

无法在python中使用Selenium Webdriver定位元素

Selenium Webdriver(Python)-无法在嵌套iframe中定位元素

即使元素 id 存在,也无法使用 selenium webdriver 定位元素

无法使用 C# 在 selenium webdriver 脚本中使用 XPath 定位元素

Selenium WebDriver:无法使用TinyMCE编辑器在iframe中定位元素

无法在python硒中使用Selenium Chrome WebDriver定位元素

无法使用Selenium WebDriver定位输入元素

无法使用 Selenium 定位 webdriver 元素 |

Ruby - Selenium Webdriver - 没有这样的元素:无法定位元素:

Selenium webdriver 消息:没有这样的元素:无法定位元素 - iFrame

无法在 selenium webdriver node.js 中定位元素错误

无法使用 Selenium Python 定位元素

无法使用Selenium Python定位元素

无法使用 Selenium 定位元素

Selenium Webdriver C# 无法使用有效的 XPath 表达式在 Chrome 中定位元素

IE 上的 Python Selenium 无法定位元素?

如何通过Python使用Selenium WebDriver在iframe中定位元素

无法使用Selenium Webdriver找到元素

定位元素的Python问题[selenium][chorme webdriver]

Selenium无法定位元素(Python)

Selenium 无法定位元素

无法在 Python 中定位元素 - Selenium

无法定位元素 _ Python Selenium

Python Selenium 无法定位元素

无法找到元素Selenium WebDriver

Selenium webdriver 无法获取元素

Selenium WebDriver无法获取元素

Selenium Webdriver,无法选择元素