【发布时间】:2018-06-20 21:25:00
【问题描述】:
如何将 html2canvas 添加到给定网页,执行它,然后使用 Watir 读取其结果?
require 'watir'
b = Watir::Browser.new :firefox
b.goto "google.com"
path = "/path/to/html2canvas.js" # v. 0.5.0-alpha1
h2c_payload = File.read(path)
b.execute_script h2c_payload
h2c_activator = %<
function genScreenshot () {
var canvasImgContentDecoded;
html2canvas(document.body, {
onrendered: function (canvas) {
window.canvasImgContentDecoded = canvas.toDataURL("image/png");
}});
};
genScreenshot();
>.gsub(/\s+/, ' ').strip
b.execute_script h2c_activator
b.execute_script "return window.canvasImgContentDecoded"
=> nil
在控制台中执行相同的 JavaScript 会导致变量(最终)被设置,然后在调用时返回。 execute_script 有什么不同?
【问题讨论】:
-
您使用 v0.5.0-alpha1 而不是最新的 v1.0.0-alpha.9 有什么原因吗?
-
@JustinKo 只是因为我找到了该版本的示例,而且似乎没有一个版本是 GM 版本。明天我会尝试更改版本。
-
最终,1.0 不起作用,但 v0.4.1 可以。感谢您举报!
标签: javascript ruby selenium-webdriver watir