【问题标题】:Problems with PhantomJS: phantom.args doesn't seem to workPhantomJS 的问题:phantom.args 似乎不起作用
【发布时间】:2012-02-24 21:14:03
【问题描述】:

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

>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

算盘.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 和 name/pass 突然失去其价值的任何帮助将不胜感激。

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

(另外,感谢 Stack Overflow 一开始就给了我大部分代码)

【问题讨论】:

  • I am running cmd.exe in C# 就是将您的问题链接到 c# 的所有内容
  • 是的,我不确定这是否与此有关
  • 我想我找到了我的问题以及解决方案:code.google.com/p/phantomjs/issues/detail?id=132
  • @dudeifea,您使用了哪种解决方案?该已发布问题中有几个建议。
  • @jlafay,我已经发布了解决方案作为问题的答案

标签: c# javascript cmd screen-scraping phantomjs


【解决方案1】:

@jlafay

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

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

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

【讨论】:

  • +1,非常感谢。希望他们将来能提供更好的方法将值传递给评估调用,以使我们不必求助于这样的黑客。
猜你喜欢
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 1970-01-01
  • 2013-02-11
  • 2016-09-23
  • 2016-11-29
  • 2016-02-01
相关资源
最近更新 更多