exposed function queryseldtcor not working in puppeteer

前端 未结 1 1145
广开言路
广开言路 2020-12-20 01:52
document.querySelectorAll(\'.summary\').innerText;

this throws error in below snippet saying \"document.querySelector is not a function\" in my pup

相关标签:
1条回答
  • 2020-12-20 02:37

    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?
    0 讨论(0)
提交回复
热议问题