一点背景......我对javascript和phantom.js有点新,所以我不知道这是一个javascript还是phantom.js bug(功能?).
以下成功完成(对于缺少的phantom.exit(),您只需要在完成后按ctrl + c):
var page = require('webpage').create();
var comment = "Hello World";
page.viewportSize = { width: 800, height: 600 };
page.open("http://www.google.com", function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
page.includeJs('http://code.jquery.com/jquery-latest.min.js', function() {
console.log("1: ", comment);
}, comment);
var foo = page.evaluate(function() {
return arguments[0];
}, comment);
console.log("2: ", foo);
}
});
Run Code Online (Sandbox Code Playgroud)
这有效:
page.includeJs('http://code.jquery.com/jquery-latest.min.js', function() {
console.log("1: ", comment);
}, comment);
Run Code Online (Sandbox Code Playgroud)
输出:1: Hello World
但不是:
page.includeJs('http://code.jquery.com/jquery-latest.min.js', function(c) { …Run Code Online (Sandbox Code Playgroud) 我无法传递参数传递给函数page.evaluate.我想提交表格.
data1 = "Textsample";
page.evaluate(function(data1) {
var form = document.getElementById ("MyForm");
form.data.value = data1;
form.submit();
});
Run Code Online (Sandbox Code Playgroud)
但是当我截取页面截图时,数据字段填充了"未定义".我做错了什么以及如何解决?