问题
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
echosomething 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
thenEvaluatecall):this.fillSelectors("form[name='IDPLogin']", { 'input[name="Ecom_User_ID"]': username, 'input[name="Ecom_Password"]': password });you may change the
thenEvaluatecall to:casper.thenEvaluate(function(usuario, senha) { // setAttribute here like above }, username, password);In your python script you don't actually use the
casperjsvariable, 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