Allow functions registered via page.exposeFunction to receive JSHandles
See original GitHub issueWhen traversing DOM tree to find elements for testing UI in order to better share code i try to register a bunch of helper functions via page.exposeFunction. Currently this api does not allow to pass JSHandles between these functions (or any other non-serializable object), which is a tough limitation.
A small sample to reproduce what I’m trying to achieve:
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.exposeFunction('arrayLength', (array) => { return array.length; });
await page.goto('https://google.com');
await new Promise(res => setTimeout(res, 1000));
const numberOfDivs = await page.evaluate(
async(selector) => await window.arrayLength(
Array.from(document.querySelectorAll(selector))),
'div');
console.log('number of divs:', numberOfDivs);
await browser.close();
It fails on some web pages (like google.com) and works on others (like example.com), what depends on whether there are circular references in DOM tree or not, which seems like not good behavior at all.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Puppeteer documentation
Puppeteer can respond to the dialog via Dialog's accept or dismiss methods. ... JSHandle instances can be passed as arguments to the page.evaluateHandle...
Read more >Puppeteer find list of shadowed elements and get ...
As this answer shows, the only way to work with this JSHandle is to run it through another evaluate , at which point...
Read more >API Reference — Pyppeteer 0.0.25 documentation
Pyppeteer allows creation of “incognito” browser context with browser. ... Registered function can be called from chrome process.
Read more >playwright/api.md
Locale will affect `navigator.language` value, `Accept-Language` request header ... function used to register a routing with [browserContext.route(url, ...
Read more >BrowserContext | Playwright 中文文档
open call, the popup will belong to the parent page's browser context. Playwright allows creation of "incognito" browser contexts with browser.newContext() ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rjktcby I think this is achievable, please point me to where I’m wrong.
You can define a set of helper functions in page context with a simple
page.evaluate
:You can call these functions like this:
What’s wrong with this approach?
I’m new here, but seems like it’d be neat to pass references between both sides. This would allow, for example, people to come up with alternatives to
Electron
, for example starting by doingpage.exposeFunction('require', ...)
.