-1

I am looking for a way to check if an element exists on a page. If it does not exist, I want to look for an other element.

Example:

I want to find the h1 on a page. If it does not exist I want to look for an h2.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
DErasmus
  • 19
  • 1
  • 7
  • 1
    Welcome to StackOverflow [tour](https://stackoverflow.com/tour). Please read and follow the posting guidelines [how to ask](https://stackoverflow.com/help/how-to-ask) – Youba Dec 31 '20 at 09:16
  • Does this answer your question? [How can I check if selector exists in puppeteer?](https://stackoverflow.com/questions/58675083/how-can-i-check-if-selector-exists-in-puppeteer) – ggorlen Aug 06 '22 at 01:12

1 Answers1

0

This works for me:

if (await page.$(‘h1‘) !== null) {
  console.log(‘Found a h1 element‘)
}
else if (await page.$(‘h2‘) !== null) {
  console.log(‘Found a h2 element‘)
}

Here is the link to the solution: https://github.com/puppeteer/puppeteer/issues/1149

DErasmus
  • 19
  • 1
  • 7