Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

node.js - Exposed function querySelector not working in Puppeteer

document.querySelectorAll('.summary').innerText;

This throws an error in the below snippet saying "document.querySelector is not a function" in my Puppeteer page's exposed fucntion docTest.

I want to pass a specific node to each method and get the result inside evaluate.

Same with document.getElemenetbyId.

const puppeteer = require('puppeteer');
//var querySelectorAll = require('query-selector');


let docTest = (document) => {
var summary = document.querySelectorAll(.summary).innerText;
console.log(summary);
return summary;
}

let scrape = async () => {

const browser = await puppeteer.launch({
    headless: false
});
const page = await browser.newPage();

await page.goto('http://localhost.com:80/static.html');
await page.waitFor(5000)
await page.exposeFunction('docTest', docTest);

var result = await page.evaluate(() => {
    var resultworking = document.querySelector("tr");
    console.log(resultworking);
    var summary  = docTest(document);
    console.log(resultworking);
    return summary;

});
console.log(result);

await page.waitFor(7000);
browser.close();
return {
    result
}
};

scrape().then((value) => {
console.log(value); // Success!
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just had the same question. The problem is that the page.evaluate() function callback has to be an async function and your function docTest() will return a Promise when called inside the page.evaluate(). To fix it, just add the async and await keywords to your code:

await page.exposeFunction('docTest', docTest);

var result = await page.evaluate(async () => {
    var summary = await docTest(document);
    console.log(summary);
    return summary;
});

Just remember that page.exposeFunction() will make your function return a Promise, then, you need to use async and await. This happens because your function will not be running inside your browser, but inside your nodejs application.

  1. exposeFunction() does not work after goto()
  2. Why can't I access 'window' in an exposeFunction() function with Puppeteer?
  3. How to use evaluateOnNewDocument and exposeFunction?
  4. exposeFunction remains in memory?
  5. Puppeteer: pass variable in .evaluate()
  6. Puppeteer evaluate function
  7. allow to pass a parameterized funciton as a string to page.evaluate
  8. Functions bound with page.exposeFunction() produce unhandled promise rejections
  9. How to pass a function in Puppeteers .evaluate() method?
  10. How can I dynamically inject functions to evaluate using Puppeteer?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...