Search code examples
javascriptnode.jsweb-scrapingxpathpuppeteer

Wait using Path (Puppeteer)


I'm using Puppeteer 22.6.0 with NodeJS for web scraping, I'm trying to pause the script until a specific h1 element is visible, the issue is there are multiple h1 elements on the page and the only possible way to identify that certain h1 is with checking if it says a certain message, like: <h1>something</h1>

Since I can't do this using a selector (as far as I know), I tried using page.waitForXPath() but I get the error:

TypeError: page.waitForXPath is not a function

I have searched for a solution but it seems the method has been removed, is there any workaround for this? Thanks.


Solution

  • You can use:

    const element = await page.waitForSelector('text/something');
    

    To narrow it down, first you can do let h1els = await page.$$('h1') - To Query all H1 elements and then do the

    await h1els.waitForSelector('text/something')
    

    You can check more in the official docs => https://pptr.dev/guides/query-selectors-legacy/#example-2