Puppeteer - Check if specific text exists

Viewed 4664

I'm using puppeteer to check for a specific text that appears on the webpage. My code to check is as follows:

if ((await page.waitForXPath('//span[contains(text(), "Subscription Confirmed")]',30000)) !== null) {
  chk = await page.evaluate(el => el.innerText, await page.$x('//span[contains(text(), "Subscription Confirmed")]'))
  chk = 'Success'
} else {
  // do something else
  chk = 'Failed'
}

This doesn't seem to detect the text for some reason. I have attached a screenshot of the DOM of the webpage where you can see the text - DOM. Hoping someone could help.

2 Answers

Try the following:

if ((await page.waitForXPath('//*[contains(text(), "Subscription Confirmed")]',30000)) !== null) {
   chk = await page.evaluate(el => el.innerText, await page.$x('//*[contains(text(), "Subscription Confirmed")]'))
   chk = 'Success'
} else { 
   chk = 'Failed'
}

This worked for me in a jest test:

  await page.waitForXPath('//*[contains(text(), "SOME TEXT")]')
Related