3

I'm trying to capture the information from a table but this table has a pagination button ("next , prev"). When I click the next button, no navigation occurs; it just creates a POST request that returns a new HTML table.

When I click the next button that causes a POST (not navigation), how can I wait to this POST to finish before trying to capture the data again from the next page?

Maybe I can detect some changes in the table element, but I don't know how.

What is the best approach for this problem?

Right now I'm doing this:

while (await page.$(NEXT_BUTTON_SELECTOR) !== null) {
  await page.click(NEXT_BUTTON_SELECTOR);
  await page.waitFor(2 * 1000);
  pageTableArray = getData();
}

but I'm not convinced that this is a good way to do it.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
GVB
  • 341
  • 3
  • 8
  • Does this answer your question? [puppeteer: Access JSON response of a specific request as in the network tab of DevTools](https://stackoverflow.com/questions/56514308/puppeteer-access-json-response-of-a-specific-request-as-in-the-network-tab-of-d) – ggorlen Nov 24 '20 at 06:17

2 Answers2

1

If await page.click(NEXT_BUTTON_SELECTOR); is causing a pageload to happen then whatever happens next in the script is lost. To get around that you'd have to do:

page.click(NEXT_BUTTON_SELECTOR); // Notice no `await`
await page.waitForNavigation();

Check out more about that API from the docs page here!

browserless
  • 2,090
  • 16
  • 16
1

You can use event requestfinished to capture data.

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', req => {
    console.log('request:', req.url())
    req.continue();
});
page.on('requestfinished', (req) => {
    console.log('finished:', req.url())
});
page.on('requestfailed', (req) => {
    console.log('failed:', req.url())
})
await page.goto(url);
await page.click(selector);
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Evan Zhang
  • 11
  • 2
  • This helps by giving you access to the response in a callback, but after clicking, if you want to access data or at least await the request completion in the promise chain that fires the click, try [`page.waitForResponse`](https://stackoverflow.com/a/56514637/6243352). – ggorlen Nov 24 '20 at 06:16