我第一次尝试使用Casper JS而且我想点击一个链接,等待ajax响应并获取一个元素值,但我似乎无法弄明白。这就是我到目前为止所做的:
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);
});
最终结果只是空白,终端没有返回任何内容。任何人都有任何关于我出错的指示?
答案 0 :(得分:0)
使用https://stackoverflow.com/users/1816580/artjom-b资源我设法使用以下方法完成:
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();