How do I access variables outside a scope inside it?

So here's my code, I'm trying to access the anum and articleHTML inside the page.evaulate scope. It says anum and articleHTML is not defined

const num = context.params.event.content.split(' ').slice(1).join(' ')
let an = parseInt(num)
let anum = an - 1
let articleHTML = an + 1

let website = ''

let browser = await puppeteer.launch();
let page = await browser.newPage()

await page.goto(website, {waitUntil: 'networkidle2'})

let data = await page.evaluate(() => {  

  articleTitle = document.getElementsByClassName("post-title h4 font-weight-bold")[anum].innerText
  author = document.getElementsByClassName("post-supertitle")[anum].innerText
  aImage = document.getElementsByClassName("img-fluid")[anum].currentSrc
  aDesc = document.getElementsByClassName("post-content font-weight-light")[anum].innerText
  artLink = document.querySelector(`a[class="ytlink"][target="_blank"][href="articles/article${articleHTML}.html"]`).href
  return {

1 answer

  • answered 2022-05-06 14:40 jongler-dev

    the reason behind this is that whatever is inside page.evaluate() run in the browser's context which is not the context of where anum and articleHTML are defined (the node.js context).

    I think you'll find the answer here: How can I pass variable into an evaluate function?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum