Puppeteer iterate over elements

Sure! Here is an example of an HTML content in a div without the body, h1, and html tags, demonstrating how you can use Puppeteer to iterate over elements:

“`html


“`

This example showcases how to use Puppeteer to iterate over elements on a webpage and extract information from them. Firstly, we launch Puppeteer and open a new page by using `puppeteer.launch()` and `browser.newPage()`. Then, we navigate to the desired webpage with `page.goto()`.

To select the necessary elements, we utilize `page.$$()` method with a specific selector. In this example, we use `’your-element-selector’`, which you should replace with your own selector. This method returns an array of elements matching the selector.

Next, we iterate over each element with a `for…of` loop, allowing us to perform actions on each element individually. Inside the loop, we extract the desired information from the element using `element.evaluate()`. In this case, we extract the `textContent` of each element.

Finally, we log the extracted information to the browser console using `console.log()`. Additionally, we store the information in a separate container with the help of `outputDiv.innerHTML`, which appends a new paragraph element for each extracted value.

Remember to replace `’https://example.com’` with the desired webpage URL and adjust the element selector accordingly to match the elements you want to iterate over.

Leave a comment