The following code doesn't work due to a ReferenceError when trying to access uid
. I understand that the eval
code is running inside the browser's context and thus doesn't have access to this variable, but I don't know how to pass the variable's value regardless:
var uid = '[email protected]';
await page.$eval('#uid', el => el.value = uid);
$$eval() method. This method runs Array. from(document. querySelectorAll(selector)) within the page and passes the result as the first argument to the pageFunction .
You cannot pass a function directly into page. evaluate() , but you can call another special method ( page. exposeFunction ), which expose your function as a global function (also available in as an attribute of your page window object), so you can call it when you are inside page.
evaluate() method. Evaluates a function in the page's context and returns the result. If the function passed to page. evaluteHandle returns a Promise, the function will wait for the promise to resolve and return its value.
The third argument of page.$eval()
passes in arguments, so you would do:
await page.$eval('#uid', (el, _uid) => el.value = _uid, uid);
page.$eval()
docs as of Puppeteer 1.3.0:
page.$eval(selector, pageFunction[, ...args])
selector
<string> A selector to query page forpageFunction
<function> Function to be evaluated in browser context...args
<...Serializable|JSHandle> Arguments to pass topageFunction
- returns: <Promise<Serializable>> Promise which resolves to the return value of
pageFunction
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With