-1

I am using the JavaScript Puppeteer API. I need to check if an element(a button) is available on a webpage so I am able to click it without the worry of it giving me an error. Sometimes the element won't appear, so knowing it's there will help me to know when to click it.

Thanks for any help!

Scroll
  • 1

1 Answers1

0

The Page.$ function will allow you query the page for an element by its selector. If it is there it will return an "element handler" if it is not present on the page it will return null. Remember to await it since this like most puppeteer functions is async.

https://pptr.dev/api/puppeteer.page._

Example:

const element = await page.$('.my-selector');
if(element !== null){
  //click on it
}

`

WillD
  • 5,170
  • 6
  • 27
  • 56