how to deal with __dopostBack using puppeteer?

39 views Asked by At

I do have a tbody which is a pagination menu. So inicially it has numbers from 1 to 10 in a td and then another td with three dots, if I click on this three dots it will generate pages from 11 to 20 and two tds with three dots, one to come back another to go forward. So I inspected it and it shows an a href inside like this: javascript:__doPostBack('dnn$ctr1205$View$grdvwlstproposicao','Page$9').

My question is, how to deal with it using puppeteer? how can I put something like page.click(dnn$ctr1205$View$grdvwlstproposicao','Page${i})?

1

There are 1 answers

0
ggorlen On

A more detailed, concrete example (preferably complete and runnable) would be nice, but I'm guessing your element looks something like this:

const puppeteer = require("puppeteer"); // ^21.4.1

const html = `<!DOCTYPE html><html><body>
<a href="javascript:__doPostBack('dnn$ctr1205$View$grdvwlstproposicao','Page$9')">click me</a>
<script>
function __doPostBack(a, b) {
  document.body.innerHTML = \`<div>\${a}</div><div>\${b}</div>\`;
}
</script>
</body></html>`;

let browser;
(async () => {
  browser = await puppeteer.launch({headless: "new"});
  const [page] = await browser.pages();
  await page.setContent(html);
  const sel = "[href=\"javascript:__doPostBack('dnn$ctr1205$View$grdvwlstproposicao','Page$9')\"]";
  await page.click(sel);
  const text = await page.$$eval("div", els => els.map(e => e.textContent));
  console.log(text); // => [ 'dnn$ctr1205$View$grdvwlstproposicao', 'Page$9' ]
})()
  .catch(err => console.error(err))
  .finally(() => browser?.close());

If you want a more general selector that isn't tied to the parameters, you can use something like '[href*="__doPostBack"]', assuming you have a unique parent container available to disambiguate between multiple of these __doPostBack links, or there's only one __doPostBack on the page. Other variants are possible depending on your specific use case.