当尝试通过casperJs连接到whatsapp网站时,我设法进入了加载页面,但此后再也没有。 我已经通过电话连接到whatsapp帐户,现在我只是在使用casper导航到页面时复制请求标头。 它成功找到了#app,但是#pane-side似乎从未加载。当通过chrome查看开发工具时,我看到在首次调用whatsapp之后发出了许多额外的请求,因此我认为casper不能正确加载js脚本或其他东西。
var casper = require('casper').create();
casper.start().thenOpen('https://web.whatsapp.com/', {
method: 'get',
headers: {
"Upgrade-Insecure-Requests": 1,
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
"X-DevTools-Emulate-Network-Conditions-Client-Id": "reallyLongHexString"
}
}, function() {
this.echo('attempting to visit whatsapp');
});
casper.wait(5000, function() {
this.echo("I've waited for 5 seconds");
})
casper.then(function() {
this.echo('First Page: ' + this.getTitle());
this.capture('0_firstpage.png');
});
casper.waitForSelector('#initial_startup',
function() {
this.echo('made it to initial_startup | startup.png');
this.captureSelector('1_startup.png', '#app');
},
function() {
this.echo('timeout failed after 5 seconds');
},
5000);
casper.waitForSelector('#app',
function() {
this.echo('made it to "app" | whatsapp.png');
this.captureSelector('2_whatsapp.png', '#app');
},
function() {
this.echo('timeout failed after 5 seconds');
},
5000);
casper.waitForSelector('#pane-side',
function() {
this.echo('made it to "pane-side" | messages.png');
this.captureSelector('3_messages.png', '#app');
},
function() {
this.echo('timeout failed after 15 seconds');
},
15000);
casper.then(function() {
this.echo('End of script: ' + this.getTitle());
this.capture('4_end.png');
});
casper.run();
上面是完整的脚本。让我知道你的想法。 谢谢