在 CasperJS 中,你如何让等待工作?
In CasperJS how do you make wait work?
为什么 'clickOldShares' 中的等待不起作用?即使我放入评估中,等待也不起作用。我相信这是一个带有参考的小细节,但我现在没有想法。
function clickOldShares(){
console.log("Waiting for Old Shares");
element = document.querySelector("#pagelet_scrolling_pager > div > div > a");
console.log("ELEMENT: " + element);
if(element){
console.log('click and waiting...');
element.click();
console.log('clicked!'+ element);
//THIS WAIT IS NOT WORKING!!!! I DON'T KNOW WHY!!!!
this.wait(2000,clickOldShares);
}
else {
console.log("done!");
}
return element;
};
//Initializing Casper
casper.start('https://www.facebook.com/', function() {
console.log("Entering on Facebook");
});
//Remote Message Handler - now I can see what happen inside evaluates
casper.on('remote.message', function(msg) {
this.echo(msg);
})
//Facebook login
casper.then(function(){
console.log("Login using username and password");
this.evaluate(function(){
document.getElementById("email").value = 'foo@bar';
document.getElementById("pass").value = 'somepass';
document.getElementById("login_form").submit();
})
if(this.exists('#u_0_2 > div:nth-child(1) > div:nth-child(1) > div > a > span')){
console.log("Login ok!");
}
else {
console.log("Login not ok!");
casper.exit();
}
});
casper.thenOpen("https://www.facebook.com/shares/view?id=1063249230414580" ,function(){
console.log("Open post with object-id");
});
casper.thenClick("#pagelet_scrolling_pager > div > div > a",function(){
this.wait(2000,function(){ this.evaluate(clickOldShares)});
});
this
页面上下文内部(casper.evaluate()
内部)指的是 window
而不是 casper
。您不能在页面上下文中使用 casper
。
您需要四处移动东西:
function clickOldShares(){
var elementAvailable = this.evaluate(function(){
console.log("Waiting for Old Shares");
element = document.querySelector("#pagelet_scrolling_pager > div > div > a");
console.log("ELEMENT: " + element);
if(element){
console.log('click and waiting...');
element.click();
console.log('clicked!'+ element);
}
return !!element;
});
if (elementAvailable) {
this.wait(2000, clickOldShares);
}
};
并像这样使用它:
casper.thenClick("#pagelet_scrolling_pager > div > div > a",function(){
this.wait(2000, clickOldShares);
});
由于无法传递 DOM 个节点,因此您需要某种表示形式表明 DOM 元素存在于页面上下文中。这可以很容易地通过将 DOM 节点强制为 !element
的布尔值然后立即取反 !!element
来检查 element
是否为 "truthy".
为什么 'clickOldShares' 中的等待不起作用?即使我放入评估中,等待也不起作用。我相信这是一个带有参考的小细节,但我现在没有想法。
function clickOldShares(){
console.log("Waiting for Old Shares");
element = document.querySelector("#pagelet_scrolling_pager > div > div > a");
console.log("ELEMENT: " + element);
if(element){
console.log('click and waiting...');
element.click();
console.log('clicked!'+ element);
//THIS WAIT IS NOT WORKING!!!! I DON'T KNOW WHY!!!!
this.wait(2000,clickOldShares);
}
else {
console.log("done!");
}
return element;
};
//Initializing Casper
casper.start('https://www.facebook.com/', function() {
console.log("Entering on Facebook");
});
//Remote Message Handler - now I can see what happen inside evaluates
casper.on('remote.message', function(msg) {
this.echo(msg);
})
//Facebook login
casper.then(function(){
console.log("Login using username and password");
this.evaluate(function(){
document.getElementById("email").value = 'foo@bar';
document.getElementById("pass").value = 'somepass';
document.getElementById("login_form").submit();
})
if(this.exists('#u_0_2 > div:nth-child(1) > div:nth-child(1) > div > a > span')){
console.log("Login ok!");
}
else {
console.log("Login not ok!");
casper.exit();
}
});
casper.thenOpen("https://www.facebook.com/shares/view?id=1063249230414580" ,function(){
console.log("Open post with object-id");
});
casper.thenClick("#pagelet_scrolling_pager > div > div > a",function(){
this.wait(2000,function(){ this.evaluate(clickOldShares)});
});
this
页面上下文内部(casper.evaluate()
内部)指的是 window
而不是 casper
。您不能在页面上下文中使用 casper
。
您需要四处移动东西:
function clickOldShares(){
var elementAvailable = this.evaluate(function(){
console.log("Waiting for Old Shares");
element = document.querySelector("#pagelet_scrolling_pager > div > div > a");
console.log("ELEMENT: " + element);
if(element){
console.log('click and waiting...');
element.click();
console.log('clicked!'+ element);
}
return !!element;
});
if (elementAvailable) {
this.wait(2000, clickOldShares);
}
};
并像这样使用它:
casper.thenClick("#pagelet_scrolling_pager > div > div > a",function(){
this.wait(2000, clickOldShares);
});
由于无法传递 DOM 个节点,因此您需要某种表示形式表明 DOM 元素存在于页面上下文中。这可以很容易地通过将 DOM 节点强制为 !element
的布尔值然后立即取反 !!element
来检查 element
是否为 "truthy".