'Selenium Webdriver: Handling pagination using while loop in Javascript

I am automating end-to-end flow for multiple questions, each of which appears on a single page. Now quiz can have 6 questions or more, as the number is not set, neither the limit. However we do have 'NEXT' button which becomes disabled when user is on the last question of the quiz.

I am trying to automate it to first select option and then click on "NEXT" button until it becomes disabled when on the last page using While loop.

Code i have tried is :

const nextButtonDisabled = driver.findElement(By.xpath("//button[@disabled and span/text()='NEXT ']"));
const nextButton = driver.findElement(By.xpath("//button/span[text() = 'NEXT ']"));
const optionButton = driver.findElement(By.xpath("(//input[@type = 'radio'])[3]"));

while(!(Quiz.nextButtonDisabled == true)) {
            
            await utils.click(Quiz.nextButton);
            await utils.click(Quiz.optionButton);
        }

Problem here is that while loop executes even after condition becomes false i.e. "Next" button becomes disabled and it errors out.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source