I have this code that basically iterates over an array of URL, and for each URL, calls the page and performs some task on the resulting HTML.
But I want the code to be completely synchronous, i.e. I want to execute all the steps sequentially: launch the browser, get the page, use the content.
I don't understand how to do that.
I'm trying to convert to async function and using await, but I can't make it work
const puppeteer = require('puppeteer');
const urlList = ["http://example.com","http://demo.com"]
for (let i = 0; i< urlList.length; i++) {
let pageContent = puppeteer
.launch()
.then(function(browser) {
return browser.newPage();
})
.then(function(page) {
return page.goto(urlList[i])
.then(function() {
return page.content();
});
})
.then(function(html){
//do something with html
console.log(html.length);
})
.catch(function(err) {
//handle error
});
}