gpt4 book ai didi

javascript - 找不到变量

转载 作者:行者123 更新时间:2023-12-03 07:09:27 25 4
gpt4 key购买 nike

我对 Phantomjs 和 JavaScript 本身很陌生,但由于某种原因,我收到错误消息:即使我声明了变量 username 并且它是一个全局变量,也无法找到它。

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");
});
});

最佳答案

evaluate
evaluate(function, arg1, arg2, ...) {object}

Evaluates the given function in the context of the web page. The execution is sandboxed [...]

http://phantomjs.org/api/webpage/method/evaluate.html

将 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

关于javascript - 找不到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36668506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com