Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer - Check if specific text exists

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.

like image 787
sayan Avatar asked Jan 30 '26 10:01

sayan


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'
}
like image 110
Igor Savinkin Avatar answered Feb 01 '26 03:02

Igor Savinkin


This worked for me in a jest test:

  await page.waitForXPath('//*[contains(text(), "SOME TEXT")]')
like image 34
Raine Revere Avatar answered Feb 01 '26 02:02

Raine Revere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!