Pass params like variable to evaluate in casperjs and login into site

一曲冷凌霜 提交于 2019-12-23 05:24:12

问题


I'm writing a python script that pass username and password like params to my casperjs script, describe below. But I don't know why a receive the error:

CasperError: casper.test property is only available using the `casperjs test` command
     C:/casperjs/modules/casper.js:179

Can someone help me about this issue?

CasperJS.py:

import os
import subprocess

# PATH to files
casperjs = 'c:\casperjs\bin\casperjs.exe'
app_root = os.path.dirname(os.path.realpath(__file__))
script = os.path.join(app_root, 'test2.js')

#  Request username and password
username = raw_input("Usuario:")
password = raw_input("Senha:")

# Username and password like
auth = [username, password] 

# Execute process casperjs via python
subprocess.Popen(['casperjs', script, auth[0], auth[1]], shell=True)

CasperJS.js:

var casper = require('casper').create({
    clientScript: ['jquery.min.js'],
    verbose: true,
    logLevel: "debug",
    // Disable load images
    pageSettings: {
    loadImages: true
    }
});

# variables
var url = 'http://minha.oi.com.br';
var username = casper.echo(casper.cli.raw.get(0));
var password = casper.echo(casper.cli.raw.get(1)); 

# start casperjs
casper.start(url);

# Try login on the website
casper.thenEvaluate(function(usuario, senha) {
document.querySelector('input#Ecom_User_ID').setAttribute('value', usuario);
document.querySelector('input#Ecom_Password').setAttribute('value', password);

}, { usuario: username, senha: password });

# Check the current URL
casper.then(function() {
        this.echo(this.getCurrentUrl());
});
casper.run();

回答1:


The issue was an old version of phantomjs that did not work properly with casperjs. The issues below provide corrections to the code in the question.


Your scripts have multiple issues

  • You can't echo something and try to assign the value in your casperjs script. So change those two lines to:

    casper.echo(casper.cli.raw.get(0));
    casper.echo(casper.cli.raw.get(1));
    
    var username = casper.cli.raw.get(0);
    var password = casper.cli.raw.get(1);
    
  • In the same script: Your comments are python comments and not js comments, so change # to //

  • In casper: You should actually select the correct input fields:

    document.querySelector('input[name="Ecom_User_ID"]').setAttribute('value', usuario);
    document.querySelector('input[name="Ecom_Password"]').setAttribute('value', senha);
    

    Or even use a function that is provided by casperjs (replacing the complete thenEvaluate call):

    this.fillSelectors("form[name='IDPLogin']", {
        'input[name="Ecom_User_ID"]': username,
        'input[name="Ecom_Password"]': password
    });
    
  • you may change the thenEvaluate call to:

    casper.thenEvaluate(function(usuario, senha) {
        // setAttribute here like above
    }, username, password);
    
  • In your python script you don't actually use the casperjs variable, but that's fine since you assume that it is in PATH.

If the error persists or a new one comes up, try updating to a newer version of phantomjs.


The resulting code might look like this:

var casper = require('casper').create();

// variables
var url = 'http://minha.oi.com.br';
var username = casper.cli.raw.get(0);
var password = casper.cli.raw.get(1);

// start casperjs
casper.start(url);

// Try login on the website
casper.then(function(){
    this.fillSelectors("form[name='IDPLogin']", {
        'input[name="Ecom_User_ID"]': username,
        'input[name="Ecom_Password"]': password
    }, true);
    // this also sents the form
});

// Check the current URL
casper.then(function() {
    this.echo(this.getCurrentUrl());
});

casper.run();


来源:https://stackoverflow.com/questions/24266164/pass-params-like-variable-to-evaluate-in-casperjs-and-login-into-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!