1 year ago

#312710

test-img

Faiz Ahmed

Puppeteer page.exposeFunction producing Error: Execution context is not available in detached frame "about:blank"

I'm building a web scraper, which has series of function calls passing around a puppeteer page object with the css selector string, to extract that html object and apply some arbitrary modification on it, provided as a function efn.

The "main" function receiving the page and doing page.evaluate:

const catElem = async ({page,selector,rwr=true,slug,efn} ={}) =>{              //efn is evaluate function bound using expose function

    if(efn){
        console.log("fn function added!");
        await page.exposeFunction("fn", x => `great ${x}`);
        
    }    
    if(page){
        
        categoryElems = await page.evaluate(async () => {
            // elem = document.querySelector(selector);
            
            if(!fn)
                return 5; //elem;
            
            return await window.fn(5);           // passed in function should return what the caller requires    #THREAT
        });
        
        if(!rwr)
            return categoryElems;
        return getShortCategories({slug: slug,pc: categoryElems});
    }
    else
        return getShortCategories({slug: slug});
      
};

This a simpler version of the function i'm using .. it receives a function efn to be bound using page.exposeFunction, like await page.exposeFunction("efn", efn) but here i've hard coded the function as x => "great ${x}" . As shown in here : How to pass a function in Puppeteers .evaluate() method?

I have another function calling this like let categoryElems = await catElem({page:page,selector:selector,efn:efn,rwr:false});

Although the catElem function returns great 5, it still produces this long ugly error, and is failing on the main thing i wanted to achieve i.e. executing the passed in function efn.

node:13021) UnhandledPromiseRejectionWarning: Error: Execution context is not available in detached frame "about:blank" (are you trying to evaluate?)
    at DOMWorld.executionContext (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:86:19)
    at DOMWorld._onBindingCalled (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:372:36)
    at /home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:55:66
    at /home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:62
    at Array.map (<anonymous>)
    at Object.emit (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:43)
    at CDPSession.emit (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/puppeteer/common/EventEmitter.js:72:22)
    at CDPSession._onMessage (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:245:18)
    at Connection._onMessage (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:117:25)
    at WebSocket.<anonymous> (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/puppeteer/lib/cjs/puppeteer/node/NodeWebSocketTransport.js:13:32)
    at WebSocket.onMessage (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (events.js:400:28)
    at Receiver.receiverOnMessage (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/ws/lib/websocket.js:1022:20)
    at Receiver.emit (events.js:400:28)
    at Receiver.dataMessage (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/ws/lib/receiver.js:522:14)
    at Receiver.getData (/home/faiz/CodeFiles/Python & Django/nwa_api/scraper/node_modules/ws/lib/receiver.js:440:17)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13021) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). 
(rejection id: 1)

(x17 ...till rejection id:17)

Any reason why this error is being produced?

EDIT: The function can be called as,

page = page.goto('https://www.foxnews.com/us/missouri-interstate-crash-five-dead');
selector = '.eyebrow > a:nth-child(1)';
efn = (x) => x.getAttribute('href');
catElem({page: page, selector:selector, rwr=false, efn=efn});

node.js

google-chrome

puppeteer

headless-browser

0 Answers

Your Answer

Accepted video resources