A page I'm trying to scrape loads with an initial body of content, followed by a "Load more" button at the bottom of the page. When the "Load more" button is clicked the button is removed from the page, the page loads additional content further down the page (preserving the content from the initial load) and a new "Load more" button is placed at the bottom of the page. The URL of the page does NOT change when the "Load more" button is clicked. I.e. it behaves like a single page application (SPA).
Using Puppeteer I'm able to navigate to the page:
let page = await browser.newPage();
await page.goto('https://www.someURL.com/home', {
waitUntil: 'domcontentloaded',
});
I then use page.$$eval()
to find the "Load more" button and click it. The page loads the additional content along with a new "Load more" button at the bottom. However I can't find a way using Puppeteer to 'refresh' my page
variable such that I could call page.eval$$()
a second time to find the new "Load more" button. Calling page.reload()
reverts the page back to the state it was in when I called page.goto()
.
I've scoured the Puppeteer docs and looked through dozens of examples and can't find a way to do this. Given the prevalence of SPAs I must be overlooking something obvious.
Is this possible?
EDIT: Additional code using @vsemozhetbyt suggestion:
let theButton = await page.$('button.sc-fzoiQi');
do {
await page.evaluate(theButton => {
theButton.click();
}, theButton);
} while ((await buttonExists(page)) !== null);
};
async function buttonExists(page) {
return await page.$('button.sc-fzoiQi');
}
Using the above, the button is clicked the first time, however the while
expression never gets evaluated. I.e. the statement inside it - return await page.$('button.sc-fzoiQi')
never returns.