PhantomJS 的问题:phantom.args 似乎不起作用

发布于 2025-01-08 20:00:17 字数 2452 浏览 1 评论 0原文

这是我的 PhantomJS 测试项目,我通过键入(例如)在 cmd.exe 中运行:

>phantomjs.exe abacus.js 1111 222
name: 1111
pass: 222
load started
load finished
jQuery loaded
console> name:
console> pass: undefined
step 0
step 1
done

Abacus.js:

var name, pass;
if (phantom.args.length !== 2) {
    console.log('not enough arguments!');
    phantom.exit();
} else {
    name = phantom.args[0];
    pass = phantom.args[1];
}
console.log("name: " + name);  //output: "name: MyUsername"
console.log("pass: " + pass);  //output: "pass: MyPassword"
var stepIndex = 0;
var page = new WebPage();
var loadInProgress = true;
var jQueryLoad = false;
page.onConsoleMessage = function (msg, line, source) {
    console.log('console> ' + msg);
};
page.onAlert = function (msg) {
    console.log('alert> ' + msg);
};
page.onLoadStarted = function () {
    loadInProgress = true;
    console.log("load started");
};
page.onLoadFinished = function () {
    loadInProgress = false;
    console.log("load finished");
    jQueryLoad = page.injectJs("jquery-1.7.1.min.js");
    if (jQueryLoad)
        console.log('jQuery loaded');
};
var interval = setInterval(function () {
    if (jQueryLoad && !loadInProgress && typeof steps[stepIndex] == "function") {
        steps[stepIndex]();
        page.render("step " + stepIndex + ".png");
        console.log("step " + stepIndex++);
    } 
    if (typeof steps[stepIndex] != "function") {
        console.log("done");
        phantom.exit(); 
    } 
}, 1000);
var steps = [
function () {
    page.evaluate(function () {
        console.log("name: " + this.name);  //output: "console> name:"
        console.log("pass: " + this.pass);  //output: "console> pass: undefined"
        var arr = document.frmMain;
        if (arr !== null) {
            arr.elements["username"].value = "MyUsername";  //Only fils in form if it's a string literal
            arr.elements["password"].value = "MyPassword";
        } else {
            console.log("Could not find frmMain");
        }
    });
}, function () {
    page.evaluate(function () {
        document.frmMain.submit();
    });
} ];
page.open("http://www.abacusdatagraphics.com/");
page.viewportSize = { width: 1280, height: 1024 };

任何有关为什么 phantom.args & 的帮助将不胜感激。名称/通行证突然失去其价值。

我在 C# 中运行 cmd.exe,因为名称和密码时不时地更改并保存在数据库中。这只是一个小测试程序,看看是否可以完成。

(另外,感谢 Stack Overflow 首先为我提供了大部分代码)

Here is my PhantomJS test project which I run in cmd.exe by typing (for example):

>phantomjs.exe abacus.js 1111 222
name: 1111
pass: 222
load started
load finished
jQuery loaded
console> name:
console> pass: undefined
step 0
step 1
done

Abacus.js:

var name, pass;
if (phantom.args.length !== 2) {
    console.log('not enough arguments!');
    phantom.exit();
} else {
    name = phantom.args[0];
    pass = phantom.args[1];
}
console.log("name: " + name);  //output: "name: MyUsername"
console.log("pass: " + pass);  //output: "pass: MyPassword"
var stepIndex = 0;
var page = new WebPage();
var loadInProgress = true;
var jQueryLoad = false;
page.onConsoleMessage = function (msg, line, source) {
    console.log('console> ' + msg);
};
page.onAlert = function (msg) {
    console.log('alert> ' + msg);
};
page.onLoadStarted = function () {
    loadInProgress = true;
    console.log("load started");
};
page.onLoadFinished = function () {
    loadInProgress = false;
    console.log("load finished");
    jQueryLoad = page.injectJs("jquery-1.7.1.min.js");
    if (jQueryLoad)
        console.log('jQuery loaded');
};
var interval = setInterval(function () {
    if (jQueryLoad && !loadInProgress && typeof steps[stepIndex] == "function") {
        steps[stepIndex]();
        page.render("step " + stepIndex + ".png");
        console.log("step " + stepIndex++);
    } 
    if (typeof steps[stepIndex] != "function") {
        console.log("done");
        phantom.exit(); 
    } 
}, 1000);
var steps = [
function () {
    page.evaluate(function () {
        console.log("name: " + this.name);  //output: "console> name:"
        console.log("pass: " + this.pass);  //output: "console> pass: undefined"
        var arr = document.frmMain;
        if (arr !== null) {
            arr.elements["username"].value = "MyUsername";  //Only fils in form if it's a string literal
            arr.elements["password"].value = "MyPassword";
        } else {
            console.log("Could not find frmMain");
        }
    });
}, function () {
    page.evaluate(function () {
        document.frmMain.submit();
    });
} ];
page.open("http://www.abacusdatagraphics.com/");
page.viewportSize = { width: 1280, height: 1024 };

Any help would be appreciated as to why phantom.args & name/pass suddenly lose their values.

I am running cmd.exe in C# since the name and password change every now and then and are kept in a database. This is simply a small test program to see if it can be done.

(Also, thanks to Stack Overflow for giving me most of this code in the first place)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2025-01-15 20:00:17

@jlafay

我使用的解决方案是这样的:

PhantomJS无法使用变量填写表单,因为 page.evaluate 无法处理参数,因此填写表单时,变量(和表单)为空。

因此,我将 function() 视为字符串并传递如下变量:

page.evaluate('function () {' +
    'var theName = ' + name + ';' +
    'var thePass = ' + pass + ';' +
    'console.log(\"name: \" + this.name);' +  //output: "console> name:"
    'console.log(\"pass: \" + this.pass);' +  //output: "console> pass: undefined"
    'var arr = document.frmMain;' +
    'if (arr !== null) {' +
    '    arr.elements["username"].value = theName;' +
    '    arr.elements["password"].value = thePass;' +
    '} else {' +
    '    console.log("Could not find frmMain");' +
    '}' +
'}');

或者类似的东西,我不再有代码了。

@jlafay

The solution I used from this is as such:

PhantomJS wasn't able to fill in the forms with variables because page.evaluate can't handle parameters so when filling out the form, the variables (and the forms) are null.

So instead I treated function() as a string and passed the variables like this:

page.evaluate('function () {' +
    'var theName = ' + name + ';' +
    'var thePass = ' + pass + ';' +
    'console.log(\"name: \" + this.name);' +  //output: "console> name:"
    'console.log(\"pass: \" + this.pass);' +  //output: "console> pass: undefined"
    'var arr = document.frmMain;' +
    'if (arr !== null) {' +
    '    arr.elements["username"].value = theName;' +
    '    arr.elements["password"].value = thePass;' +
    '} else {' +
    '    console.log("Could not find frmMain");' +
    '}' +
'}');

Or something like that, I don't have the code anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文