I am on a main page and have used evaluate to gather a list of href's
I would like to do the following from the main page
For each link:
let linksArr = [];
async function run() {
const chromeless = new Chromeless()
jobs = await chromeless
.goto('https://someurl.com')
.wait('#someId')
.evaluate(() => {
// this will be executed in headless chrome
const jobs = [].map.call(
document.querySelectorAll('a.someClass'),
function(job){
// I have a list of links, each link can be accessed individually inside map via job.href
//??? What do I do now to visit each link ??? while maintaining the same chrome window that will allow me to revert back to the main page.
return {title: job.innerText, href: job.href}
});
return JSON.stringify(jobs)
})
await chromeless.end()
}
run().catch(console.error.bind(console))
}
If jobs contains all of the title and hrefs you want to process, I would just iterate through jobs and have a function that takes the chromeless object and the job element and processes it.
Maybe something like this?
const { Chromeless } = require('chromeless')
const mainUrl = 'https://some-main-url';
async function visitSubPage(chromeless, job) {
console.log('visiting', job.href);
return chromeless
.goto(job.href);
// do stuff on the sub page
}
async function run() {
const chromeless = new Chromeless();
let jobs = await chromeless
.goto(mainUrl)
.evaluate(() => {
// this will be executed in headless chrome
const jobs = [].map.call(
document.querySelectorAll('#some-selector'),
function(job){
// I have a list of links, each link can be accessed individually inside map via job.href
//??? What do I do now to visit each link ??? while maintaining the same chrome window that will allow me to revert back to the main page.
return {title: job.innerText, href: job.href}
});
return jobs;
})
// iterate through hrefs that were found
for (var job of jobs) {
await visitSubPage(chromeless, job);
}
// once done iterating through subpages, return back to the main url
await chromeless.goto(mainUrl);
await chromeless.end()
}
run().catch(console.error.bind(console))
Thanks @youhyunkim appreciate the push in the right direction! Back to scraping :)
Thank you for that helpful response, @youhyunkim! 馃槂
Most helpful comment
If
jobscontains all of the title and hrefs you want to process, I would just iterate through jobs and have a function that takes the chromeless object and the job element and processes it.Maybe something like this?