我有我的幻影Js代码的问题,如下所示,我有代码来测试我的朋友Web服务器(使用节点Js)。实际上,它看起来很简单,完美运行。使用PhantomJs登录测试
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;
它从我的朋友Web服务器返回invalid username or password
。你能帮我弄清楚为什么会发生吗?这是我第一个'javascript世界'的代码。
我已经读过this question和another variation然后a suggestion
,但它只是让我更加迷惑。
感谢,
艾哈迈德
感谢..我会尝试明天..现在我在休息.. ^^。 –
它像河流一样工作。不管怎么说,还是要谢谢你。 –