2014-12-23 5 views
3

나는 다음과 같은 경우 작업 casperjs 얻으려고 :특정 조건이 충족 될 때까지 casperjs를 반복 할 수 있습니까?

웹 페이지가로드, 그 페이지 내에서, 그와 함께로드 데이터 항목 을 Ajax를 버튼 '자세히'이것은 다시로드 일부에서 더 많은 데이터 상품.

로드 할 데이터 항목이 많으면 '더 읽기'버튼이 있는지 반복적으로 확인하려면 스크립트가 필요합니다. 그렇다면 클릭하고 나머지 스크립트를 계속 실행하여 전체를 출력하십시오 페이지로 jpeg.

아래 코드를 작성하여 시도했지만 희망했던대로 반복하지 않습니다. 더 많은 데이터가로드되고 버튼을 다시 클릭해도 여전히 버튼을 한 번 클릭 한 다음 이미지를 출력합니다.

var casper = require('casper').create({ 
    verbose : true, 
    logLevel : 'debug', 
    pageSettings : { 
     "userAgent" : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10', 
     "loadImages" : false, 
     "webSecurityEnabled" : false, 
     "ignoreSslErrors" : true 
    }, 
}); 

var urls = { 
    login : 'http://www.website.com/login', 
    details : 'http://www.website.com/details', 
}; 

casper.start(urls.login, function() { 

    //login stuff 

}); 

//the function I'm trying to recursively loop before moving on to saving the capture image 
function checkMore() { 

    //check to see if the 'read more' button exists 
    if (casper.exists('a.read-more')) { 

     //click the button to load more items   
     casper.click('a.read-more'); 

     //wait for the items to load, then run the check again 
     casper.wait(3000, function() { 
      casper.run(checkMore); 
     }); 

    } 

} 

casper.thenOpen(urls.details, function() { 

    //wait for the page along with ajax items to load shortly after 
    this.wait(3000, function() { 
     this.run(checkMore); 
    }); 

}); 

casper.then(function() { 

    //output the result 
    this.capture('output.jpg'); 

}); 

casper.run(); 
+0

'실행'을 한 번만 호출해야한다고 생각합니다. wait 함수에 직접'checkMore'를 전달할 수 있습니다. 'this.wait (3000, checkMore)'. 그러나'checkMore'에서'this' 참조는 작동하지 않을 것입니다 - 그래서 당신은 eg를 써야합니다. 'casper.exists ('...')'. – pius

+1

'read-more' 버튼을 기다리는 동안'waitForSelector'를 고려 했습니까? 이 시나리오에 적합한 것 같습니다. http://casperjs.readthedocs.org/en/latest/modules/casper.html#waitforselector – pius

+0

어떻게 루프합니까? 문제가 무엇인지 설명하지 않았습니다. –

답변

0

피우스 (Phius)는 올바른 생각을 갖고 있었고 코드를 수정했습니다. 해결책은 아래에 있습니다.

var urls = { 
    login : 'http://www.website.com/login', 
    details : 'http://www.website.com/details', 
}; 

casper.start(urls.login, function() { 

    //login stuff 

}); 

//the function I'm trying to recursively loop before moving on to saving the capture image 
function checkMore() { 

    //check to see if the 'read more' button exists 
    if (casper.exists('a.read-more')) { 

     //click the button to load more items   
     casper.click('a.read-more'); 

     //wait for the items to load, then run the check again 
     casper.wait(3000, checkMore); 

    } 

} 

casper.thenOpen(urls.details, function() { 

    //wait for the page along with ajax items to load shortly after 
    this.wait(3000, checkMore); 

}); 

casper.then(function() { 

    //output the result 
    this.capture('output.jpg'); 

}); 

casper.run(); 
+0

'a.readmore'가 결코 보이지 않으면 어떻게 될까요? 이걸 고려하면 안돼? – Rippo

+0

Rippo, 그래서 "if (casper.exists ('a.read-more'))"로 확인합니다. 링크가 존재하지 않거나 링크가 표시되지 않으면 스크립트는 클릭하지 않으며 checkMore 함수를 다시 호출하지 않습니다. – Paul

+0

그리고 ajax 호출이 실패하면? – Rippo

관련 문제