javascript - 如何在 Java 中使用 Selenium 获取 Canvas 元素内图像的 URL,以及如何使用 JavascriptExecutor?

标签 javascript java html selenium canvas

我的具体目标是从 Canvas 容器中获取图像 URL,这是所做的:

JavascriptExecutor jse = (JavascriptExecutor) driver;

Object imageURL = jse.executeScript("arguments[0].toDataURL('image/png');",canvas);

我得到一个 null 的返回值。

然后我尝试做一些更基本的事情,比如获取 Canvas 的宽度属性。

JavascriptExecutor jse = (JavascriptExecutor) driver;
Object width= jse.executeScript("arguments[0].getAttribute('width');",canvas);

我又一次变得空了。 Canvas WebElement 由 Selenium 很好地识别,并且它的“宽度”属性存在 - 我可以使用 WebDrivergetAttribute 方法检索它。

我想我用错了。

感谢您的帮助!

最佳答案

return 关键字添加到您的脚本中。

PS:如果问题解决了请告诉我,否则我们可以尝试其他方法。

关于javascript - 如何在 Java 中使用 Selenium 获取 Canvas 元素内图像的 URL,以及如何使用 JavascriptExecutor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40510921/

相关文章:

javascript - 类型错误 : Cannot read property 'title' of undefined

java - 如何使用 Lombok ?

php - 转义 html 标签并只读取 php 中的文本?

jquery - 这里 return true 或 false 有什么区别?

javascript - jQuery.load : any way to load the same page when page is refreshed

java - 静态同步方法

java - PriorityQueue 没有正确排序其内容?

html - 在图像下定位文本和栏

html - 如何制作100%高度和固定位置的侧边栏

javascript - 加载 facebook 评论插件异步