我是Phantomjs和JavaScript本身的新手但由于某些原因我得到错误,即使我声明了变量用户名并且它是全局变量,也无法找到变量用户名。
var page = require('webpage').create();
var system = require("system");
page.open('https://www.facebook.com/login.php?login_attempt/', function(status) {
page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', function () {
var username = "test";
var password = "test";
page.evaluate(function () {
$("#email").val(username);
$("#pass").val(password);
$("#loginbutton").click();
});
page.onLoadFinished = function () {
page.render("after_submit.png");
if (page.url == "https://www.facebook.com/") {
var fs = require('fs');
var path = 'succes.txt';
var content = "Facebook : \n" + username;
fs.write(path, content, 'w');
}
phantom.exit();
};
page.render("before_submit.png");
});
});
答案 0 :(得分:1)
<强>评估强>
evaluate(function, arg1, arg2, ...) {object}
在网页的上下文中评估给定的函数。执行是沙箱[...]
将page.evaluate()视为远程位置的黑盒子。它对您的脚本一无所知,除了您专门传递给它的那些之外没有任何变量。方法如下:
var username = "test";
var password = "test";
page.evaluate(function (username, password) { // <-- here you receive variables from outside the page
$("#email").val(username);
$("#pass").val(password);
$("#loginbutton").click();
}, username, password); // <-- here you pass variables to webpage