我试图通过使用CasperJS和phantomJS 1.7.0模拟事件来从站点获取数据.
我能够模拟正常的点击事件并选择事件.但我的代码在以下场景中失败:
当我点击远程页面上的按钮/锚点等时,单击远程页面会启动一个AJAX调用/ JS调用(取决于程序员如何实现该页面).
在JS调用的情况下,我的代码工作,我得到更改的数据.但对于启动AJAX调用的点击,我没有获得更新的数据.
为了调试,我试图获取元素容器的页面源(之前和之后),但我看到代码没有变化.
我试图将等待时间从10秒设置为1毫秒,但这并不反映行为的任何变化.
下面是我点击的代码.我正在使用一个CSS路径数组,它表示要点击的元素.
/*Click on array of clickable elements using CSS Paths.*/
fn_click = function(){
casper.each(G_TAGS,function(casper, cssPath, count1)
{
casper.then ( function() {
casper.click(cssPath);
this.echo('DEBUG AFTER CLICKING -START HTML ');
//this.echo(this.getHTML("CONTAINER WHERE DETAILS CHANGE"));
this.echo('DEBUG AFTER CLICKING -START HTML');
casper.wait(5000, function()
{
casper.then(fn_getData);
}
);
});
});
};
Run Code Online (Sandbox Code Playgroud)
更新:
我尝试使用phantomJS中的remote-debug选项来调试上面的脚本.它不起作用.我在窗户上.我也会尝试在Ubuntu上运行远程调试.
请帮我.我将不胜感激任何帮助.
更新:
请查看以下代码作为示例.
https://gist.github.com/4441570
Run Code Online (Sandbox Code Playgroud)
点击前和点击后的内容相同.
我点击标签下的排序选项(投票/活动等).
这是我正在运行的脚本:
//Require CasperJS
var casper = require('casper').create();
//Scraping Courserank
var base = "https://www.courserank.com";
var home = base + "/w/home";
var schools = base + "/w/schools?switchSchool=1";
//First, navigate to homepage and login
casper.start(home, function() {
console.log('Logging in...');
//Fill in the login form
this.fill(
'form[action="login"]',
{ username : 'hatboysam@gmail.com', password : "****" },
true
);
});
function getSchools() {
var arr = document.querySelectorAll('div.link');
return arr;
}
//Go to the schools page
casper.then(function() {
console.log(this.getCurrentUrl());
//Open the school choice page
casper.open(schools).then(function() {
console.log(this.getCurrentUrl()); …Run Code Online (Sandbox Code Playgroud) 我对casperjs和javascript很新,但我在其他领域有相当丰富的编码经验.目前我正在尝试运行的代码只是访问一个网站并点击一个链接,这应该是直截了当的,但我遇到了麻烦.
var casper = require('casper').create();
var x = require('casper').selectXPath;
casper.start('http://www.guru.com/emp/search.aspx?keyword=#&&page=1&sort=Earnings');
casper.then(function() {
this.test.assertExists({
type: 'xpath',
path: '//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'
}, "Got Here");
});
casper.then(function() {
var firstUrl = this.getCurrentUrl()
});
casper.thenClick(x('//*[@class="paddingLeft5 txt11px txt666"]/a[text()="Next"]'), function() {
console.log("Woop!");
});
casper.waitFor(function check() {
return this.evaluate(function() {
return this.getCurrentUrl() != firstUrl;
});
}, function then() {
console.log(this.getCurrentUrl());
});
casper.run();
Run Code Online (Sandbox Code Playgroud)
目前这个时间超过5000毫秒而没有包装在waitFor中它只是打印两次相同的URL.
我有更多Casper麻烦,这次我告诉Casper点击一个href,它会将它重定向到一个页面,将其重定向到twitter app auth页面:
var casper = require('casper').create({
verbose: false,
logLevel: 'debug',
waitTimeout: 10000
});
phantom.cookiesEnabled = true;
casper.start('http://foo.bar', function afterstart() {
if (this.exists('#back-to-top')) {
this.echo('BOOYA! foo.bar is loaded', 'INFO');
} else {
this.echo('Page didnt load, something went all screwy.', 'ERROR');
}
});
casper.then(function signin() {
this.click('a[href="/sys/login"]' );
});
casper.then(function tellsignin() {
if (this.exists('#not-logged-in ')) {
this.echo("I clicked on twitter login button, so now the twitter login page is loaded.", 'INFO');
} else {
this.echo('Twitter login link didnt work, something went …Run Code Online (Sandbox Code Playgroud) 我无法与带有casperjs的jquery自动完成输入框进行交互.我尝试了很多不同的方法,但是当弹出选项列表时,我似乎无法选择自动完成选项.
我的代码如下:
casper.thenEvaluate(function() {
$('#myInput').val('cars'); // fill in the text box
$('#myInput').blur(); // should trigger the autocomplete ajax call
$('.ui-autocomplete li.ui-menu-item:nth-of-type(1)').click(); // should click the first item in the list
});
// take a picture to make sure it worked
casper.then(function() {
this.captureSelector('pics/test1.png', '#theForm');
});
Run Code Online (Sandbox Code Playgroud)
这根本不起作用,即使看起来应该如此.通过玩弄它,我发现触发向下箭头按键几次触发自动完成显示,所以这是一个更接近工作的版本.这适用于浏览器,但由于某种原因不适用于casper.thenEvaluate块.
$('#myInput').val('cars'); // fill in the text box
var e = jQuery.Event("keydown");
e.which = 40; // press down arrow a few times, not sure why this works
$("#myInput").trigger(e);
$("#myInput").trigger(e);
$('.ui-autocomplete li.ui-menu-item:nth-of-type(1)').click();
Run Code Online (Sandbox Code Playgroud) jquery browser-automation browser-testing jquery-autocomplete casperjs
好吧所以这是我的casperjs功能:
if(casper.exists(ac2)){
var accountnumber = this.fetchText('div.arabic:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > a:nth-child(1)');
var redir = accountnumber.substr(1);
casper.then(function() {
var uel = "https://example.ws/send.html?f=" + redir;
this.thenOpen(uel, function() {
casper.wait(10000, function() {
casper.then(function() {
var accountnumber1 = this.fetchText('div.arabic:nth-child(1) > font:nth-child(1)');
var acccc = accountnumber1.split(' ');
system.stdout.writeLine(acccc[3]); // this output a number
var amount = acccc[3];
var result = amount * 0.019;
var result2 = result.toFixed(6);
var fresult = amount - result2;
var needed = fresult.toFixed(3);
system.stdout.writeLine(needed); // this …Run Code Online (Sandbox Code Playgroud) 嗨,我想知道如何使用casperjs获取输入值
这是我的html元素
<input type="hidden" name="key" value="newkey">
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
view = 'users/registered';
casper.test.begin("Activating account", 5, function register(test){
casper.start(webroot + view, function(){
}).then(function(){
this.echo("Retrieving data from hidden input key", "COMMENT");
activationKey = this.evaluate(function() {
return __utils__.getFieldValue('key');
});
}).then(function(){
this.echo("Activating account with the key \"" + activationKey + "\"", "COMMENT");
window.location = webroot + activationKey;
});
casper.run(function() {
this.echo('Account activated successfully', 'SUCCESS').exit();
test.done();
});
});
casper.viewport(page.width, page.height);
Run Code Online (Sandbox Code Playgroud)
在这种情况下返回 null
我也尝试过:
activationKey = __utils__.getFieldValue('key');
Run Code Online (Sandbox Code Playgroud)
但是给我这个错误:
FAIL ReferenceError: Can't find variable: __utils__
Run Code Online (Sandbox Code Playgroud) 我想要做的是声明两个casper,两个将登录到某个站点,一个将充当管理员,另一个将充当典型用户.我想在一个脚本中完成它.
是否可以在单个脚本中声明两个或更多casper?就像这个:
var casper1 = require("casper").create({
verbose: true, timeout: null
});
var casper2 = require("casper").create({
verbose: true, timeout: null
});
Run Code Online (Sandbox Code Playgroud) 目前我正在使用casper.evaluate函数执行类似的操作(使用CoffeeScript)
bool = casper.evaluate ->
document.querySelector('button selector').checked
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,但我想知道是否有一个内置的casper方法,我可以用来检索复选框/无线电元素的checked属性?我尝试过使用getElementAttribute()但它不会检测到'已检查'作为属性.它也没有列在从中检索的JSON对象中getElementInfo().
我试图在我拥有的javascript文件上运行CasperJS,当我运行时
> casperjs index.js
Run Code Online (Sandbox Code Playgroud)
它说
'CasperError: Cant find module fs'.
Run Code Online (Sandbox Code Playgroud)
index.js的第一行是
var fs = require('fs');
Run Code Online (Sandbox Code Playgroud)
我安装了node,casperjs和phantomjs,为什么不能找到fs?如果我是对的,fs是Node的文件系统吗?
更新:我将nodejs添加到我的$ PATH,但仍然没有运气.