简体   繁体   中英

Puppeteer identifier string variable won't parse; Unsure why

I'm trying to have a string be used inside a puppeteer string, it won't work for some reason.

Specifically with this code

await page.waitForSelector('div[class = "sh-dlr__list-result"')

When i try to parse in a variable

let identified1 = 'div[class = "sh-dlr__list-result"'

so making

await page.waitForSelector(identified1)

It won't work. This is really limiting, is there a way around this issue?

This is the expanded code

https://jsfiddle.net/hewlbern/6p7kdozt/10/ Run it in your computer, jsfiddle unsure if I can run it from there.

I believe it is creating a cors error now - very weird: Why would using a variable create a cors error : /

Thanks!

The reason is because you're declaring identified inside the page.evaluate() . So, when you do the following it's already out of scope.

if (currentPage < pagesToScrape) {
  console.log(identified1);
  await Promise.all([
    await page.click(buttonSelector),
    await page.waitForSelector(identified),
  ]);
}

You did log the identified1 but you're using identified for the selector.

You'll have to pass the identifier2 to the pageFunction like so:

let newProducts = await page.evaluate(({ identifier2 }) => {
  // ...
},{ identifier2 });

See here some examples:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM