1

以下に示すように、ファントム Js コードに問題があります。友人の Web サーバーをテストするコードがあります (ノード Js で作成)。実際、シンプルに見えて完璧に動作します。

var page = require('webpage').create();

var address   = "http://localhost:3333";

// Route "console.log()" calls from within the Page context to the 
// main Phantom context (i.e. current "this")
page.onConsoleMessage = function(msg) {
  console.log("Console.log: ", msg);
};

page.onAlert = function(msg) {
  console.log("Alert:", msg);
};

page.open(address, function (s) {
    page.evaluate(function () {
  function click(el){
      var ev = document.createEvent("MouseEvent");
      ev.initMouseEvent(
      "click",
      true /* bubble */,
      true /* cancelable */,
      window, null,
      0, 0, 0, 0, /* coordinates */
      false, false, false, false, /* modifier keys */
      0 /*left*/, null
      );
      el.dispatchEvent(ev);
  }
  document.getElementById('username').value = 'MyName';
  document.getElementById('password').value = 'MyWord';
  click(document.querySelector('input[type=submit]'));
    });

    page.onNavigationRequested = function() {
  // console.log("Moved", JSON.stringify(arguments))
  // to check whether send or not
  page.render("printscreen" + ".png");
    };

    setTimeout(function(){
  page.render("nextprintscreen" + ".png");
        phantom.exit();
    }, 3000);
});

しかし

私が宣言するとき、
var userName = 'MyName';
var passWord = 'MyWord';
それを下に置き、
var address = "http://localhost:3333";
と交換
document.getElementById('username').value = 'MyName';
document.getElementById('password').value = 'MyWord';
します
document.getElementById('username').value = userName;
document.getElementById('password').value = passWord;

invalid username or password友人の Web サーバーから返されます。それを理解する方法と、なぜそれが起こるのか教えてください。これは私の最初の「JavaScript ワールド」コードです。

私はすでにこの質問別のバリエーションと提案を読みました

しかし、それは私をより混乱させるだけです。

ありがとう、
アフマド

4

1 に答える 1