Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

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?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...