Nicholas Jennings
Nicholas Jennings

Reputation: 211

Use puppeteer to search for element based on inner text

I've been attempting to do some web scraping using puppeteer, and I've run into the following problem: enter image description here I want to click an element that has a specific inner text (in this case 'INHERITANCE TAX RETURN'), but everything else about the element seems to be identical to a lot of other elements on the page. I was wondering if anyone knew a way to search for an element based on its inner text. Any help would be greatly appreciated.

Upvotes: 1

Views: 10743

Answers (1)

Philip Kirkbride
Philip Kirkbride

Reputation: 22889

Have you tried:

const linkHandlers = await page.$x("//span[contains(text(), 'INHERITANCE TAX RETURN')]");
if (linkHandlers.length > 0) {
  await linkHandlers[0].click();
} else {
  throw new Error("Link not found");
}

Upvotes: 4

Related Questions