1

I'm trying to use Casper JS for the first time and I'm wanting to click on a link, wait for the ajax response and the get the value an element but I cannot seem to figure it out. This is what I have so far:

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

casper.start('https://www.gumtree.com/p/3-bedrooms-rent/3-bed-mid-terrace-house-in-a-lovely-and-peaceful-part-of-hatfield-al10-area/1194478241', function() {
    this.waitForSelector( '[data-q="reply-panel-reveal-btn"]' );
});

casper.then(function() {
    casper.click( '[data-q="reply-panel-reveal-btn"]' );
});

casper.then( function(){
    phoneNumber = document.querySelector('[data-print-key="channel:syi.reveal-phone,key:data"]').innerHTML;
});

casper.run( function(){
    console.log(phoneNumber);
});

the end result is just blank, nothing returned in terminal. Anyone have any pointers in where I'm goin wrong?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Luke Snowden
  • 4,056
  • 2
  • 37
  • 70
  • 1
    Possible duplicate of [Why doesn't plain JavaScript work in CasperJS?](http://stackoverflow.com/questions/18328387/why-doesnt-plain-javascript-work-in-casperjs) – Artjom B. Oct 20 '16 at 18:48
  • If you really want to wait, then you really should use one of the `casper.wait*` functions that would be applicable in your case. – Artjom B. Oct 20 '16 at 18:49

1 Answers1

0

Using https://stackoverflow.com/users/1816580/artjom-b resource I managed to do it using the following:

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

casper.start('https://www.gumtree.com/p/3-bedrooms-rent/3-bed-mid-terrace-house-in-a-lovely-and-peaceful-part-of-hatfield-al10-area/1194478241');
casper.thenClick( '.box-padding > .clearfix > .btn-secondary.set-right ' );
casper.waitForSelectorTextChange( '.box-padding > .clearfix > .txt-large.txt-emphasis.form-row-label', function() {
    var phoneNumber = this.evaluate(function(){
        return document.querySelector( ".box-padding > .clearfix > .txt-large.txt-emphasis.form-row-label" ).textContent;
    });
    this.echo(phoneNumber);
});
casper.run(); 
Community
  • 1
  • 1
Luke Snowden
  • 4,056
  • 2
  • 37
  • 70