How to click on element with text in Puppeteer

Short answer This XPath expression will query a button which contains the text “Button text”: const [button] = await page.$x(“//button[contains(., ‘Button text’)]”); if (button) { await button.click(); } To also respect the <div class=”elements”> surrounding the buttons, use the following code: const [button] = await page.$x(“//div[@class=”elements”]/button[contains(., ‘Button text’)]”); Explanation To explain why using the text … Read more