无法让PhantomJS示例工作 [英] Not able to get PhantomJS example to work

查看:102
本文介绍了无法让PhantomJS示例工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个PhantomJS脚本,该脚本可以自动将Android应用提交到Amazon App Store。我的脚本基于这个例子:

解决方案

看起来普通登录是不可能的,我尝试使用幻像/ casperjs并且 - 不起作用。
仅适用于Cookie(并且效果很好)。

  / ********* SETTINGS * ******************** / 
var page = require('webpage')。create({viewportSize:{width:1600,height:900},
设置:{userAgent:'Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 44.0.2403.157 Safari / 537.36',
javascriptEnabled:'true',
loadImages:'false'
}}),fs = require('fs');
/ *********设置结束***************** /
console.log('加载所有设置,从执行开始);

page.onLoadStarted = function(){console.log('Loading started')};
page.onLoadFinished = function(){console.log('Loading finished')};
page.onConsoleMessage = function(msg){console.log(msg)};

//这些增值经销商,你需要有你的cookie值:

变种at_main ='AtzA的| IwEBIDds270mDqc9pBDWo49OWmIG2UAKXp8N3eYPzCuYn3fFGojBcZXag7M3uZdGurgA28cZA49zToQaoJ0DkQ8ZiAzu6EypTNzrskJNRp-IBqmKPh78s4oSU7o5oBv-7sgmjT-E8_pyxS0oC-t0SYAXdjSYzUeJbXUTTdweOVj_astxNj6_H1uqWysZw59G6sYkwq3A5JhCN_NVZszHUpmQvU4JrhAzV5a8vZgAJmFG5lnK8xl3Nk3uZNTVG3pQYhQ_SLPirg9cdDWICCQFO-fljcnad4PoPRQqdPsUw6OpCHrXEmkEDhn0EC49RzSA63bFlUKTAUUfREuiv0EXoobi19l4Ae98cq_qz38CA4UKF4OHWyBDpAZwZtgUANlaj_l82xUbWwR6yUgZByB- 7jICsIs0',
sess_at_main ='RDK4Kzj6ywmJnwhNdqkc37bjioL / FEX20dcHnjMMMUk =',
session_id ='166-7700254-1158037',
ubid_main ='157-9343046-5202510',
x_main = '?DBSZwuolSC12SSlS36cN7VhwXIg770zKumtGLK9THJji XszMxT4xe 0FNT @ MMfQ';

phantom.cookies = [{//一系列对象
'名称':'at-main',
'值':at_main,
'域名':'。。mszon'',
'路径':'/',
'hponponly':false,
'afure':true,
'到期':( new Date())。getTime()+(1000 * 60 * 60 * 43800)},// 5年//结束1
{'name':'sess-at-main',
'值':sess_at_main,
'domain':'。。syzon.com',
'路径':'/',
'hauponly':false,
'安全':true,
'expires':( new Date())。getTime()+(1000 * 60 * 60 * 43800)},//结束2
{'name':'session -id',
'值':session_id,
'domain':'。。syzon.com',
'path':'/',
'hauponly':false ,
'afure':true,
'expires':( new Date())。getTime()+(1000 * 60 * 60 * 43800)},//结束3
{ '名称:'ubid-main',
'价值':ubid_main,
'domain':'。amazon.com',
'路径':'/',
'hauponly ':false,
'afure':true,
'expires':( new Date())。getTime()+(1000 * 60 * 60 * 43800)},// 4 of $
{'name':'x-main',
'value':x_main,
'domain':'。。mszon'',
'path':'/' ,
'httponly':false,
'afure':true,
'expires':( new Date())。getTime()+(1000 * 60 * 60 * 43800)} //结束5
]

//逐步执行步骤
page.open(https://developer.amazon.com/home.html,功能(状态){
console.log('Step 1');
page.render('step1.png');
setTimeout(step2,3000);
function step2(){
console.log(Step 2);
page.render('step2.png');
var result = page.evaluate(function(){return document.documentElement.outerHTML});
fs.write('AmazonLoggedIn.html',result,'w'); phantom.exit();
}
});



您可能还需要设置让我登录 chekbox:



更新:

为了避免登录地狱,您可以使用 - cookies-文件选项!
第二次尝试后,你的脚本就像一个魅力!!

  ./ phantomjs --cookies-file =。 /cookies_2.txt am.js luxadm1@gmail.com传递> / dev / stdout 


I am trying to write a PhantomJS script that will automate Android app submission to the Amazon App Store. I am basing my script on this example: http://code-epicenter.com/how-to-login-amazon-using-phantomjs-working-example/. I have modified that example to work with https://developer.amazon.com/home.html instead of the URL used in the example.

Here is what I have tried:

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 webPage = require('webpage');
var page = webPage.create();
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;

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 ( username == 'unknown' ) {
        console.log('Please specify username and password');
        phantom.exit();
}
if ( password == 'unknown' ) {
        console.log('Please specify username and password');
        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');
                page.open("https://developer.amazon.com/home.html", 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 );
            page.evaluate(function(username,password){
                    console.log( '  username is ' + username );
                    document.getElementById("ap_email").value=username;
                    document.getElementById("ap_password").value=password;
                    document.getElementById("ap_signin_form").submit();
            },username, password);
    },


        /*
         * Step 3 - Wait Amazon to login user. After user is successfully logged in,
         * user is redirected to home page. Content of the home page is saved to AmazonLoggedIn.html.
         * You can find this file where phantomjs.exe file is. You can open this file using Chrome to ensure that you are logged in.
         */
        function(){
                console.log("Step 3 - blah blah blah");
                var fs = require('fs');
                var result = page.evaluate(function() {
                        return document.querySelectorAll("html")[0].outerHTML;
                });
                fs.write('AmazonLoggedIn.html',result,'w');
        },
];

/**********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 {
            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);
};

When I run the script output does get written to the AmazonLoggedIn.html file but the contents indicate that the login failed. I am pretty certain that I am using the correct credentials. Here is what the resulting AmazonLoggedIn.html looks like:

解决方案

Looks like plain login is impossible, i have tried with phantom/casperjs and - doesn't work. Only works with Cookie (and works perfectly).

/*********SETTINGS*********************/
var page = require('webpage').create({viewportSize:{width: 1600,height: 900},
settings:{userAgent:'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',
javascriptEnabled:'true',
loadImages:'false'
}}), fs = require('fs');
/*********SETTINGS END*****************/
console.log('All settings loaded, start with execution');

page.onLoadStarted = function() { console.log('Loading started') };
page.onLoadFinished =function() { console.log('Loading finished') };
page.onConsoleMessage = function(msg) { console.log(msg) };

// in these vars, you need to have the values of your cookies:

var at_main='Atza|IwEBIDds270mDqc9pBDWo49OWmIG2UAKXp8N3eYPzCuYn3fFGojBcZXag7M3uZdGurgA28cZA49zToQaoJ0DkQ8ZiAzu6EypTNzrskJNRp-IBqmKPh78s4oSU7o5oBv-7sgmjT-E8_pyxS0oC-t0SYAXdjSYzUeJbXUTTdweOVj_astxNj6_H1uqWysZw59G6sYkwq3A5JhCN_NVZszHUpmQvU4JrhAzV5a8vZgAJmFG5lnK8xl3Nk3uZNTVG3pQYhQ_SLPirg9cdDWICCQFO-fljcnad4PoPRQqdPsUw6OpCHrXEmkEDhn0EC49RzSA63bFlUKTAUUfREuiv0EXoobi19l4Ae98cq_qz38CA4UKF4OHWyBDpAZwZtgUANlaj_l82xUbWwR6yUgZByB-7jICsIs0',
sess_at_main='RDK4Kzj6ywmJnwhNdqkc37bjioL/FEX20dcHnjMMMUk=',
session_id='166-7700254-1158037',
ubid_main='157-9343046-5202510',
x_main='DBSZwuolSC12SSlS36cN7VhwXIg770zKumtGLK9THJji?XszMxT4xe?0FNT@MMfQ';

phantom.cookies = [{// an array of objects
  'name'     : 'at-main',   
  'value'    :  at_main,
  'domain'   : '.amazon.com',
  'path'     : '/',
  'httponly' : false,
  'secure'   : true,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60 * 43800) }, //5 years // end of 1 
{ 'name'     : 'sess-at-main',   
  'value'    :  sess_at_main,
  'domain'   : '.amazon.com',
  'path'     : '/',
  'httponly' : false,
  'secure'   : true,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60 * 43800) }, // end of 2
{ 'name'     : 'session-id',
  'value'    :  session_id,
  'domain'   : '.amazon.com',
  'path'     : '/',
  'httponly' : false,
  'secure'   : true,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60 * 43800) }, // end of 3
{ 'name'     : 'ubid-main',   
  'value'    :  ubid_main,
  'domain'   : '.amazon.com',
  'path'     : '/',
  'httponly' : false,
  'secure'   : true,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60 * 43800) }, // end of 4
{ 'name'     : 'x-main',
  'value'    :  x_main,
  'domain'   : '.amazon.com',
  'path'     : '/',
  'httponly' : false,
  'secure'   : true,
  'expires'  : (new Date()).getTime() + (1000 * 60 * 60 * 43800) } // end of 5
]

//Execute steps one by one
page.open("https://developer.amazon.com/home.html", function(status){
console.log('Step 1');
                     page.render('step1.png');
                                             setTimeout(step2,3000);
function step2(){
console.log("Step 2");
                     page.render('step2.png');
                                            var result=page.evaluate(function(){return document.documentElement.outerHTML});
                                                                     fs.write('AmazonLoggedIn.html',result,'w'); phantom.exit();
}
});

step1.png

// in these vars, you need to have the values of your cookies:

var at_main=' ',
sess_at_main=' ',
session_id=' ',
ubid_main=' ',
x_main=' ';

You probably also need to set the Keep me signed in chekbox:

Update:
To avoid that hell with the login, you can use --cookies-file option!
After the second attempt, your script works like a charm !!

./phantomjs --cookies-file=./cookies_2.txt am.js luxadm1@gmail.com pass >/dev/stdout

这篇关于无法让PhantomJS示例工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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