I've been attempting to do some web scraping using puppeteer, and I've run into the following problem:  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.
 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.
            Asked
            
        
        
            Active
            
        
            Viewed 1.0k times
        
    1
            
            
         
    
    
        Nicholas Jennings
        
- 201
- 2
- 3
- 8
- 
                    2Possible duplicate: https://stackoverflow.com/questions/47407791/puppeteer-click-on-element-with-text – Antonio Narkevich Jun 04 '18 at 08:41
- 
                    2Possible duplicate of [Puppeteer: Click on element with text](https://stackoverflow.com/questions/47407791/puppeteer-click-on-element-with-text) – Vaviloff Jun 04 '18 at 09:28
1 Answers
4
            
            
        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");
}
 
    
    
        Philip Kirkbride
        
- 21,381
- 38
- 125
- 225