Skip to content

How to click a link with a specific text with Puppeteer

FULL-STACK WEB DEVELOPMENT BOOTCAMP

2024 COHORT SIGNUPS END TOMORROW

I wanted to click an β€œAccept all” cookie button with Puppeteer, I used this code:

const [linkcookie] = await page.$x("//a[contains(., 'Accept all')]")
if (linkcookie) {
  await linkcookie.click()
}

Note that if the button is a button HTML element (it depends on the HTML markup used), you have to use

page.$x("//button[contains(., 'Accept all')]")

instead πŸ‘

Also see my full Puppeteer tutorial

β†’ Get my Node.js Handbook
β†’ Read my Node.js Tutorial
  • Learn modern web development in my BOOTCAMP (SIGNUP END TOMORROW FEB 20, 2024)
  • THE VALLEY OF CODE (+ PRO), your web development manual
  • I wrote 15+ coding BOOKS, all available in THE VALLEY OF CODE PRO
  • Indie solopreneur internet business masterclass SOLO LAB (summer 2024)