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?
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();