如何在PhantomJS中调用按钮的onClick()方法? [英] How to call a button's onClick() method in PhantomJS?

查看:108
本文介绍了如何在PhantomJS中调用按钮的onClick()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不重复。我在



我应该注意的另一件事是我可以通过Chrome浏览器的开发者控制台让按钮工作。



更新1:



谢谢Igor,但我仍然无法点击按钮。这是整个脚本:

  // 
//像这样运行这个脚本:
//
// phantomjs --cookies-file = cookys.txt example_amazon_login.js'myemailaddress@gmail.com''mypasswordshhhh'
//
//您可能需要执行两次脚本,以便cookys.txt文件获取写入的数据。
//请参阅https://stackoverflow.com/questions/41391254/not-able-to-get-phantomjs-example-to-work
//


var steps = [];
var testindex = 0;
var loadInProgress = false; //当页面仍在加载时设置为true

/ ********* SETTINGS ********* ************ /
var username ='unknown';
var password ='unknown';
var apptitle ='unknown';
var category ='音乐&音频;

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

/ *
page.onResourceRequested = function(request){
console.log('Request'+ JSON.stringify(request,undefined,4));
};
page.onResourceReceived = function(response){
console.log('Receive'+ JSON.stringify(response,undefined,4));
};
* /

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.settings.userAgent ='Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 44.0.2403.157 Safari / 537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false; //将此字段设置为false,脚本速度更快
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
/ ********* SETTINGS END ***************** /


/ *获取命令line args用户密码* /
var system = require('system');
var args = system.args;
var initial_url ='https://developer.amazon.com/login.html';

if(args.length === 1){
console.log('尝试在调用此脚本时传递一些参数!');
} else {
args.forEach(function(arg,i){
console.log(i +':'+ arg);
if(i === 1 ){username = arg;}
if(i === 2){password = arg;}
if(i === 3){apptitle = arg;}
});
}

if(username =='unknown'){
console.log('请指定用户名和密码');
phantom.exit();
}
if(密码=='未知'){
console.log('请指定用户名和密码');
phantom.exit();
}
if(apptitle =='unknown'){
console.log('请指定apptitle');
phantom.exit();
}


console.log('所有设置已加载,以执行开始');
page.onConsoleMessage = function(msg){
console.log(msg);
};
/ **********定义应该做的定义步骤*********************** /
步骤= [

/ *
*第1步 - 打开亚马逊主页
* /
function(){
console.log('第1步 - 打开亚马逊主页'+ initial_url);
// page.open(https://developer.amazon.com/home.html,函数(状态){
page.open(initial_url,function(status){
console.log('status is'+ status);
});
},

/ *
*第2步 - 填写并提交登录表单
* /
函数(用户名,密码){
console.log('第2步 - 填充并提交登录表单');
// var appActionToken = page.evaluate( function(){return $('input [name =appActionToken]')。attr('value');});
// console.log('appActionToken is'+ appActionToken);
console.log('username is'+ username);
console.log('password is'+ password);
page.evaluate(function(username,password){
console。 log('用户名是'+用户名);
console.log('密码是'+密码);
document.getElementById(ap_email)。value = username;
document.getElementById(ap_password)。value = password;
document.getElementById(ap_signin_form)。submit();
},用户名,密码);
},
/ *
*步骤3点击add_new_app按钮
* /
function(){
console.log('Step 3 - Click在add_new_app按钮');
page.evaluate(function(){
var evnt = document.createEvent(MouseEvents);
evnt.initEvent(click,true,true);
document .getElementById(add_new_app_link)。dispatchEvent(evnt);

});
//page.render('step3.png');
},

/ *
*步骤4 - 填充并提交First App提交的vorm
*
*< input id =submit_button type =submitclass =button large primary one-click-submitname =savevalue =saveonclick =sanitizeManifestURL()>
*
*试试这里:
* https://stackoverflow.com/questions/32771609/how-to-click-on-selectbox-options-using-phantomjs
*
* /

功能(apptitle,category,click){
console.log('Step 4 - save app'+ apptitle);
page.evaluate(function(apptitle,category,click){
document.getElementById('title')。value = apptitle;
var sel = document.querySelector('select');
sel.selectedIndex = 16;
//这个工作
var evt = document.createEvent(HTMLEvents);
evt.initEvent(change,false,true) ;
sel.dispatchEvent(evt);

//通过点击按钮提交表格:
click('#submit_button');
},apptitle,类别,点击);

// setTimeout(function(){
page.render('step4.png');
// console.log(page.content);
//},200);
}
];

/ ********** FANTOM应该做的最后步骤*********************** /

//逐个执行步骤
interval = setInterval(executeRequestsStepByStep,50);
函数click(sel){
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click',1,1,window,1,0,0,0,0,0,0,0,0,0,null);
document.querySelector(sel).dispatchEvent(event);
}

函数executeRequestsStepByStep(){
if(loadInProgress == false&& typeof steps [testindex] ==function){
console。 log(testindex is+ testindex);
if(testindex == 1){
console.log(username is+ username);
步[testindex](用户名,密码);
} else if(testindex == 3){
steps [testindex](apptitle,category,click);
} else {
steps [testindex]();
}
testindex ++;
}
if(typeof steps [testindex]!=function){
console.log(test complete!);
phantom.exit();
}
}

/ **
*这些听众非常重要,以便幻想正常工作。
*使用这些监听器,我们控制loadInProgress标记,它控制,完全加载页面。
*如果没有这个,我们将获得页面的内容,即使页面没有完全加载。
* /
page.onLoadStarted = function(){
loadInProgress = true;
console.log('Loading started');
};
page.onLoadFinished = function(){
loadInProgress = false;
console.log('Loading finished');
};
page.onConsoleMessage = function(msg){
console.log(msg);
};


解决方案

PhantomJS在工作完成之前退出。

我们需要在步骤完成后等待。

  // 
//运行此脚本像这样:
//
// phantomjs --cookies-file = cookys.txt example_amazon_login.js'myemailaddress@gmail.com''mypasswordshhhh'
//
//您可能需要执行两次脚本,以便cookys.txt文件将数据写入其中。
//请参阅http://stackoverflow.com/questions/41391254/not-able-to-get-phantomjs-example-to-work
//


var steps = [];
var testindex = 0;
var loadInProgress = false; //当页面仍在加载时设置为true

/ ********* SETTINGS ********* ************ /
var username ='unknown';
var password ='unknown';
var apptitle ='unknown';
var category ='音乐&音频;

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

/ *
page.onResourceRequested = function(request){
console.log('Request'+ JSON.stringify(request,undefined,4));
};
page.onResourceReceived = function(response){
console.log('Receive'+ JSON.stringify(response,undefined,4));
};
* /

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.settings.userAgent ='Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 44.0.2403.157 Safari / 537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false; //将此字段设置为false,脚本速度更快
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
/ ********* SETTINGS END ***************** /


/ *获取命令line args用户密码* /
var system = require('system');
var args = system.args;
var initial_url ='https://developer.amazon.com/login.html';

if(args.length === 1){
console.log('尝试在调用此脚本时传递一些参数!');
} else {
args.forEach(function(arg,i){
console.log(i +':'+ arg);
if(i === 1 ){username = arg;}
if(i === 2){password = arg;}
if(i === 3){apptitle = arg;}
});
}

if(username =='unknown'){
console.log('请指定用户名和密码');
phantom.exit();
}
if(密码=='未知'){
console.log('请指定用户名和密码');
phantom.exit();
}
if(apptitle =='unknown'){
console.log('请指定apptitle');
phantom.exit();
}


console.log('所有设置已加载,以执行开始');
page.onConsoleMessage = function(msg){
console.log(msg);
};
/ **********定义应该做的定义步骤*********************** /
步骤= [

/ *
*第1步 - 打开亚马逊主页
* /
function(){
console.log('第1步 - 打开亚马逊主页'+ initial_url);
// page.open(https://developer.amazon.com/home.html,函数(状态){
page.open(initial_url,function(status){
console.log('status is'+ status);
});
},

/ *
*第2步 - 填写并提交登录表单
* /
函数(用户名,密码){
console.log('第2步 - 填充并提交登录表单');
// var appActionToken = page.evaluate( function(){return $('input [name =appActionToken]')。attr('value');});
// console.log('appActionToken is'+ appActionToken);
console.log('username is'+ username);
console.log('password is'+ password);
page.evaluate(function(username,password){
console。 log('用户名是'+用户名);
console.log('密码是'+密码);
document.getElementById(ap_email)。value = username;
document.getElementById(ap_password)。value = password;
document.getElementById(ap_signin_form)。submit();
},用户名,密码);
},
/ *
*步骤3点击add_new_app按钮
* /
function(){
console.log('Step 3 - Click在add_new_app按钮');
page.evaluate(function(){
var evnt = document.createEvent(MouseEvents);
evnt.initEvent(click,true,true);
document .getElementById(add_new_app_link)。dispatchEvent(evnt);

});
//page.render('step3.png');
},

/ *
*步骤4 - 填充并提交First App提交的vorm
*
*< input id =submit_button type =submitclass =button large primary one-click-submitname =savevalue =saveonclick =sanitizeManifestURL()>
*
*试试这里:
* http://stackoverflow.com/questions/32771609/how-to-click-on-selectbox-options-using-phantomjs
*
* /
函数(apptitle,category){
console.log('Step 4 - save app'+ apptitle);
page.evaluate(function(apptitle,category){
document.getElementById('title')。value = apptitle;
//这可以
document.querySelector('select ')。selectEdex = 16;
$('select [lvl =1]')。change();
//document.querySelector('select[lvl=\"2]') .selectedIndex = 1;
// $('select [lvl =2]')。change();

//通过点击按钮提交表格:
$('#submit_button')。click();
},apptitle,category);

page.render('step4.png');
// console.log(page.content);
},
];

/ ********** FANTOM应该做的最后步骤*********************** /

//逐个执行步骤
interval = setInterval(executeRequestsStepByStep,50);

函数executeRequestsStepByStep(){
if(loadInProgress == false&& typeof steps [testindex] ==function){
console.log(testindex is + testindex);
if(testindex == 1){
console.log(username is+ username);
步[testindex](用户名,密码);
}否则if(testindex == 3){
步[testindex](apptitle,category);
} else {
steps [testindex]();
}
testindex ++;
}
if(typeof steps [testindex]!=function){
//我们需要等待,在步骤完成后!
clearInterval(interval); interval = 0;
setTimeout(function(){
console.log(test complete!);
page.render('complete.png');
setTimeout(phantom.exit, 2000)
},3000);

}
}

/ **
*这些听众非常重要,以便幻想正常工作。
*使用这些监听器,我们控制loadInProgress标记,它控制,完全加载页面。
*如果没有这个,我们将获得页面的内容,即使页面没有完全加载。
* /
page.onLoadStarted = function(){
loadInProgress = true;
console.log('Loading started');
};
page.onLoadFinished = function(){
loadInProgress = false;
console.log('Loading finished');
};
page.onConsoleMessage = function(msg){
console.log(msg);
};


This is NOT a duplicate. I have looked at the answers in PhantomJS; click an element and the OP said that the answer was to call the "onClick()" method assigned to element that needs to be clicked, but the OP does NOT say HOW they accomplished this. Also there are no accepted answers.

I am trying to automate the submission of Android apps to Amazon's App store with a PhantonJS script. I have these steps so far:

  1. Open the developer console URL: https://developer.amazon.com/login.html
  2. Login to my developer account
  3. Click on the add_new_app_link
  4. Populate the First App submission form and then call the onclick method defined on this element:

    < input id="submit_button" type="submit" class="button large primary one-click-submit" name="save" value="Save" onclick="sanitizeManifestURL()">

It is step 4 that is giving trouble. Here's the code:

    function(apptitle,category){
            page.evaluate(function(apptitle,category){
                    document.getElementById('title').value=apptitle;
                    var sel = document.querySelector('select');
                    sel.selectedIndex = 16;
                    sanitizeManifestURL();
            },apptitle,category);
            page.render('step4.png');
            console.log(page.content);
    },

I dump out the resulting HTML with console.log() and it is rather long so I created this pastebin: http://pastebin.com/kjg6XqSW

Here is the screen shot I generate.

Another thing I should note is that I can get the button to work via Chrome browser's developer console.

UPDATE 1:

Thanks Igor, but I am still not able to get the button to click. Here's the whole script:

//
//  Run this script like so:
//
//       phantomjs --cookies-file=cookys.txt example_amazon_login.js 'myemailaddress@gmail.com' 'mypasswordshhhh'
//
//       You may need to execute the script twice so that the cookys.txt file gets data written to it.
//       See https://stackoverflow.com/questions/41391254/not-able-to-get-phantomjs-example-to-work
//


var steps=[];
var testindex = 0;
var loadInProgress = false;//This is set to true when a page is still loading

/*********SETTINGS*********************/
var username = 'unknown';
var password = 'unknown';
var apptitle = 'unknown';
var category = 'Music & Audio';

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

/*
page.onResourceRequested = function(request) {
  console.log('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function(response) {
 console.log('Receive ' + JSON.stringify(response, undefined, 4));
};
*/

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.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
/*********SETTINGS END*****************/


/* Get command line args user password*/
var system = require('system');
var args = system.args;
var initial_url = 'https://developer.amazon.com/login.html';

if (args.length === 1) {
  console.log('Try to pass some arguments when invoking this script!');
} else {
  args.forEach(function(arg, i) {
    console.log(i + ': ' + arg);
    if ( i === 1 ) { username = arg; }
    if ( i === 2 ) { password = arg; }
    if ( i === 3 ) { apptitle = arg; }
  });
}

if ( username == 'unknown' ) {
        console.log('Please specify username and password');
        phantom.exit();
}
if ( password == 'unknown' ) {
        console.log('Please specify username and password');
        phantom.exit();
}
if ( apptitle == 'unknown' ) {
        console.log('Please specify apptitle');
        phantom.exit();
}


console.log('All settings loaded, start with execution');
page.onConsoleMessage = function(msg) {
    console.log(msg);
};
/**********DEFINE STEPS THAT FANTOM SHOULD DO***********************/
steps = [

        /*
         * Step 1 - Open Amazon home page
         */
        function(){
                console.log('Step 1 - Open Amazon home page ' + initial_url);
                // page.open("https://developer.amazon.com/home.html", function(status) {
                page.open( initial_url, function(status) {
                        console.log('status is '+ status );
                });
        },

        /*
         * Step 2 - Populate and submit the login form
         */
        function(username,password){
                console.log('Step 2 - Populate and submit the login form');
                // var appActionToken = page.evaluate(function() { return $('input[name="appActionToken"]').attr('value'); });
                // console.log( 'appActionToken is ' + appActionToken );
                console.log( 'username is ' + username );
                console.log( 'password is ' + password );
                page.evaluate(function(username,password){
                        console.log( '  username is ' + username );
                        console.log( '  password is ' + password );
                        document.getElementById("ap_email").value=username;
                        document.getElementById("ap_password").value=password;
                        document.getElementById("ap_signin_form").submit();
                },username, password);
        },
        /*
         * Step 3 Click the add_new_app button
         */
        function(){
                console.log('Step 3 - Click on the add_new_app button');
                page.evaluate(function(){
                        var evnt = document.createEvent("MouseEvents");
                        evnt.initEvent("click",true,true);
                        document.getElementById("add_new_app_link").dispatchEvent(evnt);

                });
                //page.render('step3.png');
        },

        /*
         * Step 4 - Populate and submit the First App submission vorm
         *
         * <input id="submit_button" type="submit" class="button large primary one-click-submit" name="save" value="Save" onclick="sanitizeManifestURL()">
         *
         * try looking here:
         * https://stackoverflow.com/questions/32771609/how-to-click-on-selectbox-options-using-phantomjs
         *
         */

        function(apptitle,category,click){
                console.log('Step 4 - save app ' + apptitle);
                page.evaluate(function(apptitle,category,click){
                        document.getElementById('title').value=apptitle;
                        var sel = document.querySelector('select');
                        sel.selectedIndex = 16;
                        // this works
                        var evt = document.createEvent("HTMLEvents");
                        evt.initEvent("change", false, true);
                        sel.dispatchEvent(evt);

                        //The form will be submitted, by click on the button:
                        click('#submit_button');
                },apptitle,category,click);

//              setTimeout(function(){
                        page.render('step4.png');
//                      console.log(page.content);
//              },200);
        }
];

/**********END STEPS THAT FANTOM SHOULD DO***********************/

//Execute steps one by one
interval = setInterval(executeRequestsStepByStep,50);
function click(sel){
        var event=document.createEvent('MouseEvents');
        event.initMouseEvent('click',1,1,window,1,0,0,0,0,0,0,0,0,0,null);
        document.querySelector(sel).dispatchEvent(event);
}

function executeRequestsStepByStep(){
    if (loadInProgress == false && typeof steps[testindex] == "function") {
        console.log("testindex is " + testindex );
        if ( testindex == 1 ) {
                console.log( "username is " + username );
                steps[testindex](username, password);
        } else if ( testindex == 3 ) {
                steps[testindex](apptitle, category, click);
        } else {
            steps[testindex]();
        }
        testindex++;
    }
    if (typeof steps[testindex] != "function") {
        console.log("test complete!");
        phantom.exit();
    }
}

/**
 * These listeners are very important in order to phantom work properly.
 * Using these listeners, we control loadInProgress marker which controls, weather a page is fully loaded.
 * Without this, we will get content of the page, even a page is not fully loaded.
 */
page.onLoadStarted = function() {
    loadInProgress = true;
    console.log('Loading started');
};
page.onLoadFinished = function() {
    loadInProgress = false;
    console.log('Loading finished');
};
page.onConsoleMessage = function(msg) {
    console.log(msg);
};

解决方案

PhantomJS exits before the work is done.
We need to wait, after the steps is complete.

//
//  Run this script like so:
//
//       phantomjs --cookies-file=cookys.txt example_amazon_login.js 'myemailaddress@gmail.com' 'mypasswordshhhh'
//
//       You may need to execute the script twice so that the cookys.txt file gets data written to it.
//       See http://stackoverflow.com/questions/41391254/not-able-to-get-phantomjs-example-to-work
//


var steps=[];
var testindex = 0;
var loadInProgress = false;//This is set to true when a page is still loading

/*********SETTINGS*********************/
var username = 'unknown';
var password = 'unknown';
var apptitle = 'unknown';
var category = 'Music & Audio';

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

/*
page.onResourceRequested = function(request) {
  console.log('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function(response) {
 console.log('Receive ' + JSON.stringify(response, undefined, 4));
};
*/

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.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
/*********SETTINGS END*****************/


/* Get command line args user password*/
var system = require('system');
var args = system.args;
var initial_url = 'https://developer.amazon.com/login.html';

if (args.length === 1) {
  console.log('Try to pass some arguments when invoking this script!');
} else {
  args.forEach(function(arg, i) {
    console.log(i + ': ' + arg);
    if ( i === 1 ) { username = arg; }
    if ( i === 2 ) { password = arg; }
    if ( i === 3 ) { apptitle = arg; }
  });
}

if ( username == 'unknown' ) {
        console.log('Please specify username and password');
        phantom.exit();
}
if ( password == 'unknown' ) {
        console.log('Please specify username and password');
        phantom.exit();
}
if ( apptitle == 'unknown' ) {
        console.log('Please specify apptitle');
        phantom.exit();
}


console.log('All settings loaded, start with execution');
page.onConsoleMessage = function(msg) {
    console.log(msg);
};
/**********DEFINE STEPS THAT FANTOM SHOULD DO***********************/
steps = [

        /*
         * Step 1 - Open Amazon home page
         */
        function(){
                console.log('Step 1 - Open Amazon home page ' + initial_url);
                // page.open("https://developer.amazon.com/home.html", function(status) {
                page.open( initial_url, function(status) {
                        console.log('status is '+ status );
                });
        },

        /*
         * Step 2 - Populate and submit the login form
         */
        function(username,password){
                console.log('Step 2 - Populate and submit the login form');
                // var appActionToken = page.evaluate(function() { return $('input[name="appActionToken"]').attr('value'); });
                // console.log( 'appActionToken is ' + appActionToken );
                console.log( 'username is ' + username );
                console.log( 'password is ' + password );
                page.evaluate(function(username,password){
                        console.log( '  username is ' + username );
                        console.log( '  password is ' + password );
                        document.getElementById("ap_email").value=username;
                        document.getElementById("ap_password").value=password;
                        document.getElementById("ap_signin_form").submit();
                },username, password);
        },
        /*
         * Step 3 Click the add_new_app button
         */
        function(){
                console.log('Step 3 - Click on the add_new_app button');
                page.evaluate(function(){
                        var evnt = document.createEvent("MouseEvents");
                        evnt.initEvent("click",true,true);
                        document.getElementById("add_new_app_link").dispatchEvent(evnt);

                });
                //page.render('step3.png');
        },

        /*
         * Step 4 - Populate and submit the First App submission vorm
         *
         * <input id="submit_button" type="submit" class="button large primary one-click-submit" name="save" value="Save" onclick="sanitizeManifestURL()">
         *
         * try looking here:
         * http://stackoverflow.com/questions/32771609/how-to-click-on-selectbox-options-using-phantomjs
         *
         */
        function(apptitle,category){
                console.log('Step 4 - save app ' + apptitle);
                page.evaluate(function(apptitle,category){
                        document.getElementById('title').value=apptitle;
                        // this works
                       document.querySelector('select').selectedIndex = 16;
                        $('select[lvl="1"]').change();
                        //document.querySelector('select[lvl="2"]').selectedIndex = 1;
                        //$('select[lvl="2"]').change();

                        //The form will be submitted, by click on the button:
                        $('#submit_button').click();
                   },apptitle,category);

                        page.render('step4.png');
//                      console.log(page.content);
        },
];

/**********END STEPS THAT FANTOM SHOULD DO***********************/

//Execute steps one by one
interval = setInterval(executeRequestsStepByStep,50);

function executeRequestsStepByStep(){
    if (loadInProgress == false && typeof steps[testindex] == "function") {
        console.log("testindex is " + testindex );
        if ( testindex == 1 ) {
                console.log( "username is " + username );
                steps[testindex](username, password);
        } else if ( testindex == 3 ) {
                steps[testindex](apptitle, category);
        } else {
            steps[testindex]();
        }
        testindex++;
    }
    if (typeof steps[testindex] != "function") {
  //We need to wait, after the steps is complete!
   clearInterval(interval);interval=0;
   setTimeout(function(){
   console.log("test complete!");
   page.render('complete.png');
   setTimeout(phantom.exit,2000)
   },3000);

    }
}

/**
 * These listeners are very important in order to phantom work properly.
 * Using these listeners, we control loadInProgress marker which controls, weather a page is fully loaded.
 * Without this, we will get content of the page, even a page is not fully loaded.
 */
page.onLoadStarted = function() {
    loadInProgress = true;
    console.log('Loading started');
};
page.onLoadFinished = function() {
    loadInProgress = false;
    console.log('Loading finished');
};
page.onConsoleMessage = function(msg) {
    console.log(msg);
};

这篇关于如何在PhantomJS中调用按钮的onClick()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆