But this approach doesn’t work because the button doesn’t actually have a value attribute. I get an error saying no element was found with that selector. What’s the correct way to select and interact with buttons based on their displayed text content in puppeteer?
Try using page.waitForSelector with the ::-p-text() pseudo-selector instead. It’s built specifically for text-based selection in Puppeteer and I’ve found it way more reliable than XPath:
This waits for a button with the exact text “Settings” then clicks it. Works great for dynamic content that loads after the page renders. It plays nicer with Puppeteer’s waiting mechanisms and performs better than XPath for text selections.
When you need to select a button element by its visible text using Puppeteer, XPath is your best option. Your previous approach with the value attribute won’t work since the button lacks that attribute. Instead, you can use the following XPath syntax to target the button by its text:
This code snippet searches for any button element that contains the text ‘Settings’. It’s robust against variations like extra whitespace or inner elements, so ensure you check if the button exists to avoid errors.