我很长一段时间以来一直试图这样做。 [PHANTOMJS VERSION] => 2.1.1 我的目标是登录门户,记录后转到不同的菜单,插入参数到研究可用文件,如果还没下载则下载文件。 问题是我可以登录门户网站并访问索引页面,但是当我点击标签时,#34; a"这是一个按钮,它带我到新的表单填写搜索文件,它带我回到登录页面...不知道为什么会发生这种情况。如果我在chrome dev中使用这个脚本它可以正常工作......
登录成功
重定向登录页面 这是我的代码:
<pre><code>var page = require('webpage').create();
var system= require('system');
var loadInProgress = false;
var testindex = 0;
var address;
var pos;
var stringa;
// Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onError = function(msg, trace) {
var msgStack = ['ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
page.onAlert = function(msg) {
console.log('alert!!> ' + msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("CARICAMENTO PAGINA INIZIATO"); //*Page started loading*
};
page.onLoadFinished = function(status) {
loadInProgress = false;
if (status !== 'success') {
console.log('Caricamento pagina fallito !'); //*Page failed to load*
phantom.exit();
} else {
console.log("CARICAMENTO PAGINA COMPLETATO"); //*Page loading complete*
page.render("foto" + testindex + ".png");//*Screen Capture*
console.log("Foto effettuata");
var sitoattuale= page.evaluate(function(){
return window.location.href;
});
console.log(sitoattuale);
}
};
var steps = [
function() {
page.open(system.args[1]);
},
function() {
page.evaluate(function() {
//*insert username and password and click the button to submit the form*
document.getElementById("twsTemplate_Content1_twsModule_txtUser").value="MYUSERNAME";
document.getElementById("twsTemplate_Content1_twsModule_txtPSW").value="MYPASSWORD";
document.getElementById("twsTemplate_Content1_twsModule_btnLogin").click();
});
//*Capture inserted credentials*
page.render("credenziali.png")
},
function() {
page.evaluate(function(){
var a=document.getElementById("twsTemplate_Header1_Mainmenu2_aspMainMenun16Items");
var b= a.getElementsByTagName("a");
var c= b[0];
c.click();
});
//*Capture click of the button that brings me to the form where I ve to insert parameters for the search*
page.render("Cliccato_ricerca.png")
},
function() {
//*I capture the screen as I should be to the form page but it caputre the login page unfilled*
page.render("ramomisura.png")
console.log("stouscendo");
}
];interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
if (testindex==0){
console.log("Fase " + (testindex+1) + " : Avvio portale");
}else{
if(testindex==1){
console.log("Fase " + (testindex+1) + " : Loing in corso ...");
}else{
if(testindex==2){
console.log("Fase " + (testindex+1) + " : Tentativo di navigazione al ramo misure ...")
}else{
console.log("Fase " + (testindex+1) + " : Termine Programma");
}
}
}
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("Programma Terminato");
phantom.exit();
}
}, 100)
</code></pre>