0

I use casperjs for grab some test.

Algorithm is open URL parse page, click button for load next page. How to grab next pages until test is complete. All question get random and I don't now next question before form submitted.

I need parse pages like a cycle or recursive. My code is:

casper.start(startUrl, function () { this.click('#training'); this.evaluate(function () { $('input[type="submit"]:first').click(); }); }); casper.then(function () { var currentUrl = this.getCurrentUrl(), startIdPos = currentUrl.indexOf('=') + 1, questionId = currentUrl.slice(startIdPos), content = $(this.getHTML()), answers = [], question, startCorrectAnswerPos = content.find('script:nth-child(2)').html().indexOf('var bc='), correctAnswer = content.find('script:nth-child(2)').html().slice(startCorrectAnswerPos + 8, startCorrectAnswerPos + 9); question = content.find('table.quizz p.qw').html(); console.log(">>>>>>" + this.getCurrentUrl()); this.fill('form', { 'answer': correctAnswer }, true); }); casper.run(); 

This code complete parse only one page, but doesn't redirect to next page and parse it. What I do wrong?

1
  • Here is good tutorial explaining how to handle pagination with CasperJS: code-epicenter.com/… Commented Oct 18, 2015 at 15:37

1 Answer 1

3

EDIT: You need to nest the steps for the following pages, because on every page you evaluate if it is necessary to go further. Also you should check the URL after you submitted the form.

function answer() { var currentUrl = this.getCurrentUrl(), startIdPos = currentUrl.indexOf('=') + 1, questionId = currentUrl.slice(startIdPos), content = $(this.getHTML()), answers = [], question, startCorrectAnswerPos = content.find('script:nth-child(2)').html().indexOf('var bc='), correctAnswer = content.find('script:nth-child(2)').html().slice(startCorrectAnswerPos + 8, startCorrectAnswerPos + 9); question = content.find('table.quizz p.qw').html(); console.log(">>>>>>" + this.getCurrentUrl()); if (question) { this.then(function(){ this.fill('form', { 'answer': correctAnswer }, true); }); this.then(answer); } }; casper.then(answer); 

Exchange this code for your casper.then block.


Previous Answer: I don't know what kind of button/link #training is, but it may be that you need to wait for the change in the page to occur. You could use the casper.waitForSelector function.

Also I'm not sure why you write

this.evaluate(function () { $('input[type="submit"]:first').click(); }); 

and not simply this.click('input[type="submit"]:first');.

Sign up to request clarification or add additional context in comments.

5 Comments

this is first page like "start test" (this.click('#training');), after that I need press <button> for get next question. I should submit form before detect selector which indicate that the test is over.
You mean that the casper.then block is executed for the first page correctly?
Yes first page is correct, but after that I need submit page several times (for example 20 times) and url does not change.
Thank's a lot. How easy it is!
Keep in mind that you cannot do anything in the same recursion level after you called the next recursion or it will break your brain.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.