0

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 = 'https://newkrunktimes.github.io/'

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 {
    articleTitle,
    author,
    aImage,
    aDesc,
    artLink
    }
  
})
  • 1
    How are you running this code? Are you running the entire file at once? When run this code I get no such (not defined) error – Nick Kress May 06 '22 at 00:52
  • `anum` isn't visible in browser context. Pass it to evaluate. See [How can I pass variable into an evaluate function?](https://stackoverflow.com/questions/46088351/how-can-i-pass-variable-into-an-evaluate-function) – ggorlen May 06 '22 at 15:59

1 Answers1

0

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?