【发布时间】:2016-05-15 17:53:37
【问题描述】:
我通常是 PhantomJS 和 JS 的新手,无法理解为什么 querySelector 无法访问我的元素全局变量。我试图在各种 function() 中传递元素但无济于事。我在下面包含了一份未经篡改的代码副本。
var page = require('webpage').create();
var args = require('system').args;
var uri = args[1];
var element = args[2];
console.log('Arg 1: '+args[1]);
console.log('Arg 2: '+args[2]);
page.open(uri, function (status) {
if (status !== 'success') {
console.log('Error Opening Page');
} else {
window.setTimeout(function () {
var bounds = page.evaluate(function () {
canvas = document.querySelector('#'+ element);
return canvas.getBoundingClientRect();
});
page.clipRect = {
top: bounds.top,
left: bounds.left,
width: bounds.width,
height: bounds.height
};
page.render('result.png');
phantom.exit();
}, 500);
}
});
结果
$ ./phantomjs test.js http://fabricjs.com/static_canvas c
Arg 1: http://fabricjs.com/static_canvas
Arg 2: c
ReferenceError: Can't find variable: args
undefined:3
:6
TypeError: null is not an object (evaluating 'bounds.top')
phantomjs://code/test.js:23
【问题讨论】:
标签: javascript phantomjs parameter-passing