[Playwright] How to get the result from the executeScript() run

Dear all,
I would like to get the result from the executeScript().
For example, when running the script in the console:

document.getElementsByTagName("input")[2].value  // will show the result "xxxxxx"

so I would like to get the result “xxxxxx” from

let result = await I.executeScript(async () =>
    document.getElementsByTagName("input")[2].value);

but failed (result is null or empty).

Could you kindly help to advise how to get the executeScript() result?

Thanks.

i think the async is wrong and also you should return the value inside the script.

Try:

let result = await I.executeScript( () =>
    return document.getElementsByTagName("input")[2].value);

We are using a function inside a custom helper class, that returns a value:

  async grabUserAgentFromActiveBrowser() {
    const userAgent = await this.helpers.WebDriver.executeScript(
      "return navigator.userAgent;"
    );

so it must be possible somehow! :wink:

EDIT: sorry, i didn’t read that you’re using playwright, so maybe what i’ve wrote does not work for you cause we’re using WebDriver. But just try and let me (us) know if it works or not. thx

1 Like

Thanks for your reply.