A very simple script driving me mad

40 views
Skip to first unread message

Malc Fowler

unread,
Feb 28, 2017, 2:46:15 PM2/28/17
to CasperJS
Hi all,

New to Casperjs so excuse the stupid question.

I am trying to automate the retreival of data from www.iagcargo.com.  The functionality is quite simple,  complete two fields on a form and click the search button. (In future these will be passed as arguments)

I have copied someone elses code to make sure the script is running in the correct order. Step four will not run.  I have tried various ways to click the SEARCH button on this web page to no avail. Can anyone help?

Thanks


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

var x = require('casper').selectXPath;

casper
.userAgent('Mozilla/4.0 (compatable; MSIE 6.0; Windows NT 5.1)');

casper
.start();

casper
.then(function step1() {
   
this.echo('this is step two');
});

casper
.thenOpen('https://www.iagcargo.com/iagcargo/portlet/en/html/601/main/search', function step2() {
   
this.echo('this is step 2 - IAG is open');
});

casper
.then(function step3() {
   
this.echo('this is step Three');
   
this.sendKeys('#awb_cia','125');
   
this.sendKeys('#awb_cod','36486914');    
   
});

casper
.then(function step4() {
    casper
.click(x('//*[contains(text(),"'+SEARCH+'")]'));
   
        waitForText
('Booked on flight',function(){
       
this.echo('this is step five');
        casper
.capture('iag2.png');        
   
});
   
});


require('utils').dump(casper.steps.map(function(step) {
   
return step.toString();
}));

   
casper
.run();



Message has been deleted

tim nolet

unread,
Mar 1, 2017, 5:41:11 PM3/1/17
to CasperJS
Hi,

I think the stray "waitForText" method is causing trouble. Also, your Xpath selector is a bit verbose. Here is a working version of your script. Hope it helps!

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

var x = require('casper').selectXPath;

casper.userAgent('Mozilla/4.0 (compatable; MSIE 6.0; Windows NT 5.1)');

casper.start();

casper.then(function step1() {
    this.echo('this is step two');
});

    this.echo('this is step 2 - IAG is open');
});

casper.then(function step3() {
    this.echo('this is step Three');
    this.sendKeys('#awb_cia','125');
    this.sendKeys('#awb_cod','36486914'); 
});

casper.then(function step4() {
this.echo('step 4')
    this.click(x('//*[@value="Search"]'));     
    this.waitForText('Booked on flight', function(){

Malc Fowler

unread,
Mar 2, 2017, 6:44:34 AM3/2/17
to CasperJS
Tim,

Thank you so much for that, I had spent hours on it nd can't believe I was so close. I've been programming in PHP for years but I'm finding the Casper Javascript a bit challenging at the moment.

Regards
Malcolm

tim nolet

unread,
Mar 2, 2017, 10:14:36 AM3/2/17
to CasperJS
Np! CasperJs is pretty cool and powerful, but the error handling and logging can be a bit spartan!

Ken

unread,
May 28, 2017, 10:10:56 PM5/28/17
to CasperJS
Not sure if something like this would be useful for you, I made a general purpose web automation tool that is base purely on CasperJS. It's designed for non-developers, so it is very simple to use as developers. Basically converts natural-like language into working JS code to run CasperJS. Error-checking, live mode, Chrome extension etc etc... I use it for rapid prototyping and development of web automation.

http://github.com/tebelorg/TagUI
Reply all
Reply to author
Forward
0 new messages