问题描述
我正在编写一个Python脚本,该脚本将用户名和密码(如params)传递给我的casperjs脚本,如下所述.但是我不知道为什么会收到错误:
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:
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:
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();
推荐答案
问题是phantomjs的旧版本无法与casperjs一起正常使用.以下问题对问题代码进行了更正.
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.
您的脚本有多个问题
-
您不能
echo
尝试在casperjs脚本中分配值.因此,将这两行更改为:
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);
在同一脚本中:您的注释是python注释,而不是js注释,因此将#
更改为//
In the same script: Your comments are python comments and not js comments, so change #
to //
在casper中:您实际上应该选择正确的输入字段:
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);
或者甚至使用casperjs提供的功能(替换完整的thenEvaluate
调用):
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
});
您可以将thenEvaluate
调用更改为:
you may change the thenEvaluate
call to:
casper.thenEvaluate(function(usuario, senha) {
// setAttribute here like above
}, username, password);
在您的python脚本中,您实际上并未使用casperjs
变量,但这很好,因为您假定它在PATH中.
In your python script you don't actually use the casperjs
variable, but that's fine since you assume that it is in PATH.
如果错误仍然存在或出现新错误,请尝试更新为phantomjs的较新版本.
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();
这篇关于传递参数(如变量)以在casperjs中评估并登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!