Selecting Puppeteer Elements by Index or Matching Inner Text

Query: How can I obtain a Puppeteer element handle by its array position or by matching its text?

Example:

const elements = await page.getElements('.sample-item');
const handleByIndex = elements[1];

const handleByText = elements.find(el => el.textContent === 'Desired Content');

I recently ran into a similar problem while automating tests on a complex table layout and I found that combining several strategies helped. Usually, I first retrieve the list of elements using page.$$ or document.querySelectorAll and then iterate over each element using page.evaluate. This way, I can extract the text content reliably. Sometimes it is more straightforward to target elements based on attributes if matching by text gets too clunky with varying whitespace. I also experimented with filtering the elements array in Node code, which made handling indexes consistent for the targeted elements.