'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 = '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
}
})
Solution 1:[1]
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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jongler-dev |