0

I am still a bit new to JavaScript. I am trying to dynamically change the title of a page through javascript. However I am getting a result of undefined. I have tried multiple things and still no luck. What is the right way to pass a value to this anonymous function?

let email = "myemail@email.com"
await page.evaluate(  (email) => document.title = email );
console.log(await page.title());
MaryCoding
  • 624
  • 1
  • 9
  • 31

1 Answers1

2

I never used playwright js, but reading this documentation page here, https://playwright.dev/docs/evaluating, it looks like you pass arguments for the anonymous function as the second, and next parameters, of the evaluation method:

let email = "myemail@email.com"
await page.evaluate( (email) => document.title = email, email );
console.log(await page.title());
TheFRedFox
  • 600
  • 3
  • 12
  • This answered my issue perfectly! – MaryCoding Dec 25 '21 at 00:19
  • Also never used it, but presumably you can also use a closure, voiding the need for parameters in the first place: `await page.evaluate(() => document.title = email)` – Amadan Dec 25 '21 at 00:20
  • 1
    @Amadan nah it runs in a different execution context, so it doesn't have access to external variables – skara9 Dec 25 '21 at 00:21
  • @MaryCoding glad it helped. :) – TheFRedFox Dec 25 '21 at 00:22
  • Amadan and skara9 I am wondering that, too. Might be interesting to just try it. It's not impossible for a programming language to pass variables like that to inline functions. However, if it works here for playwright, I have no idea. ^^ – TheFRedFox Dec 25 '21 at 00:26