1

以下は、入力して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 に感謝します)

4

1 に答える 1

1

@jlafay

これから使用したソリューション次のとおりです。

page.evaluate はパラメーターを処理できないため、PhantomJS はフォームに変数を入力できませんでした。そのため、フォームに入力すると、変数 (およびフォーム) が null になります。

その代わりに、 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");' +
    '}' +
'}');

またはそのようなもの、私はもうコードを持っていません。

于 2012-05-22T19:23:58.877 に答える