Access javascript window variables in Puppeteer
P粉787806024
P粉787806024 2023-09-04 23:32:41
0
1
501

In my Puppeteer script, I use file JS to execute before loading the page:

const preloadFile = fs.readFileSync('./file.js', 'utf8'); const id = await page.evaluateOnNewDocument(preloadFile); 

file.js contains:

const i = setInterval(()=>{ if (window.turnstile) { clearInterval(i) window.turnstile.render = (a,b) => { b.userAgent = navigator.userAgent window.tsCallback = b.callback window.action = b.action window.data = b.cData console.log(b) return 'foo' } } },50) 

This script prints an object (Action, Cdata, Callback...) on the browser console

I want to get this object from puppeter side and be able to call function callback().

I used page.evaluate() but it didn’t work for me

P粉787806024
P粉787806024

reply all (1)
P粉193307465

You can use my Puppeteer wrapper libraryhttps://github.com/wix-incubator/bubanai/pullsto achieve this goal (get the object from the browser console), usenew ConsoleDriver(context).getMessages()method, and then convert the received object from the array to JSON...

Or just copy the ConsoleDriver code into your project.

refer to

Usage examples

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!