Chromeless: How to properly visit a list of links ??

Created on 21 Aug 2017  路  3Comments  路  Source: prisma-archive/chromeless

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:

  1. goto(link)
  2. collect some data (I already have the logic)
  3. goto('mainpage.com')
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))
  }
question

Most helpful comment

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))

All 3 comments

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! 馃槂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maruidea picture maruidea  路  6Comments

georgejor picture georgejor  路  5Comments

kellylawrence picture kellylawrence  路  4Comments

just-boris picture just-boris  路  5Comments

ylscsdb picture ylscsdb  路  6Comments