是否可以为“ for”循环在CasperJS中? [英] Is it possible to make "for" loop in CasperJS?
问题描述
这是
当我单击下一页时,您会看到
[上一页] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [下一页]
但[12]的Xpath不是 // * [@ id = content] / div [3] / a [12]
--->
// * [@ id = content] / div [3] / a [2]
所以我必须迭代函数execOnce
包括代码 casper.wait(6000,function(){}
因为我的目标网站对查询确实很敏感,因此我会尽可能地放置等待代码。.
在这种情况下,我可以使用这样的嵌套函数吗?
函数execOnce(casper,i,max){
if(i === max){
函数execOnce(casper,i,max){
return;
}
...
XPath非常具有表现力。例如,您可以根据链接文本而不是链接位置来选择目标页面链接( // div [@ class ='paginate'] / a [text()='5']
),但是在这种情况下,仅此一项并不能为您带来太大帮助。
问题当然是该网站具有辅助分页功能。您需要先转到下一个分页页面,然后才能单击下一个分页链接。
casper.wait(3000,function(){
var nextButton = x('// * [@ id = content] / div [3] / a [text()='+ i +' ]');
var lastPageNextButton ='.paginate> strong + a.next';
var button = nextButton;
if(this.exists(lastPageNextButton)){
button = lastPageNextButton;
}否则if(!this.exists(button)){
this.echo(i + not available);
return;
}
this.thenClick(button,function(){
console.log('Searching dic');
words = words.concat(this.evaluate(getWords) );
//递归步骤
execOnce(this,i + 1,max);
});
});
It is additional question of How to stop a loop when clicking asynchronously in CasperJS
I tried this code
function execOnce(casper, i, max){
// end condition
if (i === max) {
return;
}
casper.wait(3000, function() {
var button = x('//*[@id="content"]/div[3]/a['+i+']');
if (!this.exists(button)) {
this.echo(i + " not available");
return;
}
this.thenClick(button, function (){
console.log('Searching dic');
words = words.concat(this.evaluate(getWords));
// recursive step
execOnce(this, i+1, max);
});
});
};
// start the recursive chain
casper.then(function(){
execOnce(this, 1, 200);
});
But I found that indexes' Xpath of my target web pages has iteration.
When it reached '//*[@id="mArticle"]/div[2]/a['11']'
next index's Xpath becomes '//*[@id="mArticle"]/div[2]/a['2']
(back to a['2'])
for example the webpage url is "http://krdic.naver.com/search.nhn?query=%E3%85%8F%E3%85%8F&kind=keyword"
under the page there are [1][2][3][4][5][6][7][8][9][10] [Next Page]
When I click Next page you can see
[Previous Page][11][12][13][14][15][16][17][18][19][20] [Next Page]
but [12] 's Xpath is not //*[@id="content"]/div[3]/a[12]
---> It is
//*[@id="content"]/div[3]/a[2]
So I have to do iteration of function execOnce
including code casper.wait(6000, function() {}
because my target website is really sensitive to query so I put "wait" code whenever I can..!
In case of this can I use nested function like this?
function execOnce(casper, i, max){
if (i === max) {
function execOnce(casper, i, max){
return;
}
...
XPath is very expressive. You can for example select the intended page link based on link text instead of link position (//div[@class='paginate']/a[text()='5']
), but that alone doesn't help you much in this case.
The problem is of course that the site has a secondary pagination. You need to go to the next pagination page, before you can click of the next pagination links.
casper.wait(3000, function() {
var nextButton = x('//*[@id="content"]/div[3]/a[text()="'+i+'"]');
var lastPageNextButton = '.paginate > strong + a.next';
var button = nextButton;
if (this.exists(lastPageNextButton)) {
button = lastPageNextButton;
} else if (!this.exists(button)) {
this.echo(i + " not available");
return;
}
this.thenClick(button, function (){
console.log('Searching dic');
words = words.concat(this.evaluate(getWords));
// recursive step
execOnce(this, i+1, max);
});
});
这篇关于是否可以为“ for”循环在CasperJS中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!