4

None of the existing Q&As have a clean solution for this question, e.g.,

So here is a simple working example for people to see/try/fix:

const puppeteer = require('puppeteer');

(async() => {
  const browser = await puppeteer.launch({headless: true});
  try {
    const page = await browser.newPage();

    await page.setViewport({width: 800,height: 800});
    const response = await page.goto(`https://stock.finance.sina.com.cn/usstock/quotes/TSLA.html`, {
        waitUntil: 'networkidle0'
      });
    //console.log(await page.content());

    let selector = `div.kke_menus_tab_edage > div:nth-child(6) > a`
    await page.waitForSelector(selector)
    await page.click(selector)

    const inputElement = await page.$('div.sec.clearfix div.block.block_hq')
    await inputElement.screenshot({path: 'sina-finance.png'})

  } catch (err) {console.log(err.message);}
  finally {
    await browser.close();
  }
})()

So, in summary,

  • The click does not trigger any navigation
  • The inputElement already exist, even before clicking so can't wait on that.

Any simple solution like page.waitForNavigation, e.g., page.waitForNetwork({waitUntil: 'networkidle0'})?
Else, I'll raise a bug to the puppeteer project to provide one. Thx.

xpt
  • 20,363
  • 37
  • 127
  • 216

1 Answers1

1

If you know the specific request being made when you click the button, you can easily use the page.waitForResponse() method.

e.g.

const pageClick = page.click(selector);
const response = page.waitForResponse(response => response.url() === 'https://example.com' && response.status() === 200);

await Promise.all([
    pageClick,
    response
]);
Seane
  • 41
  • 1
  • 6
  • 1
    The `response.url()` has to be very specific, right? Unfortunately, if that's the case, it won't work for my case, as the sub request is firing up to `/e.gif?UATrack||170.1xx.1xx.6_1579617169.47||170.1xx.1xx.6_1579617169.48||||chart_detail||k_re0||||||||&gUid_1579617220029` which is changing all the times. I.e., I need a generic solution, not a specific one. – xpt Jan 21 '20 at 14:43
  • If you can see a pattern somewhere, this can work, as `response.url()` returns a string. You can easily use the `include` method or regex to match what you're looking for. Unfortunately, I don't see any way of achieving what you desire. – Seane Jan 22 '20 at 09:03