Finding text on the page in the Chromium tab open with Puppeteer

Viewed 25

I have a currently working project in puppeteer. Finally, I want it to search for words on the page that is open on the tab in the Chromium window that is open on the screen, but the open page, not a specific link. If there is any one to search for more than one word, if there is no "one of the words found" in ture.txt, it needs to print a sentence such as "not found any of the words" in false.txt and it will do this operation one after the other every time, how can I do it?

1 Answers

Hei, first of all u need an array with all ur keywords, then u can search with:

const wordsToSearch = ['firstword', 'secondword', 

'thirdword', 'fourthword'];

const contentPage = await page.$eval('body', el => el.textContent);
const result = contentPage.split(/\s+/).filter(text => wordsToSearch.includes(text));

in result you'll have all the words found.

Related