Warm tip: This article is reproduced from serverfault.com, please click

Using function parameter as a CSS selector in Puppeteer

发布于 2020-11-28 22:53:54

at the bottom is the full code.

Basically, I'm trying to make a function where the css selector is defined in the function paramater. (This is //Defining Scrape Candidate Function). When using the code at the bottom, i get

"UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: css is not defined"

Everything else works fine.

It also works if i regularly insert the css selector, so if i use the below code it works.

document.queryselector('.entry-1[style="box-sizing: border-box; float: left; position: relative; z-index: 104; height: 84.25px; width: 478.156px; border-top-style: none; border-left-style: none; display: block;"]')

I would just copy and paste this line of code, but i want to repeat this function with different css selectors on the web page.

I'm not sure how to fix it.

const { email, password } = require('./config.json');
const { NS, ES ,SS ,NI, WW, EW ,NTW, CUML, DU, HNY, WY, SY, NM, SM, MER, MID, DN, LLR, SW, STAF, BIRM, CD, SOM, DGW, BNO, BS, HIOW, CSN, BH, ESX, SSX, K, NL, EL, WL, CL } = require('./regions.json');
const { selector1, selector2 } = require('./selectors.json');

const puppeteer = require('puppeteer');

(async () => {
//New Session
    const browser = await puppeteer.launch({headless: false, ignoreHTTPSErrors: true})
    const page = await browser.newPage()

//Defining Login Function
async function Login() {
    await page.goto('https://www.respublica.co/login');
    await page.type('input[type="email"]', email);
    await page.type('input[type="password"]', password);
    await page.click('button');
    await page.waitForNavigation();
    console.log('Succesfully Logged In as:', page.url());
}

//Defining Navigation Function
async function Navigate(region) {
    await page.goto(region);
    console.log('Navigated to', page.url());
    await page.waitForSelector('[style="position: absolute; box-sizing: border-box; z-index: 104; height: 300px; top: 0px; overflow-y: scroll; border-radius: 0px; width: 478.156px; left: 0px;"]');
}

//Defining Scroll Function
async function Scroll() {
    for (let step = 0; step < 5; step++) {
    await page.click('[style="position: absolute; box-sizing: border-box; z-index: 104; height: 300px; top: 0px; overflow-y: scroll; border-radius: 0px; width: 478.156px; left: 0px;"]');
    await page.waitForTimeout(500);
    await page.keyboard.press('Space');}
}

//Defining Scrape Candidate Function

async function FindCandidate(css) {
    let candidate1 = await page.evaluate(
        () =>  document.querySelector(css).innerText);
        console.log(candidate1);
}
//Run this code
try{
    await Login()
  } catch(err){
    console.log("Failed to log in");
  }

await Navigate(SM)
await Scroll()
await FindCandidate('.entry-1[style="box-sizing: border-box; float: left; position: relative; z-index: 104; height: 84.25px; width: 478.156px; border-top-style: none; border-left-style: none; display: block;"]')

})();
Questioner
George.M
Viewed
0
CyberT33N 2020-11-29 07:53:45

Code inside of evaluate is executed in your DOM and this means css is not defined because css is currently only inside of your Node.js APP. To include variables to the DOM you must import them.

const FindCandidate = async css=>{

  const candidate1 = await page.evaluate(css=>{ 
    console.log('This will be print in your browser console.. css: ' + css);
    return document.querySelector(css).textContent;
  }, css);

  console.log(candidate1);

};