Playwright JavaScript Execution
This chapter covers how to execute JavaScript code in Playwright, including page context execution, argument passing, and init script injection.
page.evaluate() page context execution
page.evaluate() Execute code in the page's JavaScript environment, with access to the DOM and page global variables.
Example
test
(
'Get page data'
,
async
(
{
page
}
)
=>
{
await page.
goto
(
'https://www.runoob.com/'
)
;
// Get the page title (executed in the page context)
const
title
=
await page.
evaluate
(
(
)
=>
{
return
document.
title
;
}
)
;
console.
log
(
'Page title:'
,
title
)
;
// Get the number of all links on the page
const
linkCount
=
await page.
evaluate
(
(
)
=>
{
return
document.
querySelectorAll
(
'a'
)
.
length
;
}
)
;
console.
log
(
'Number of links:'
,
linkCount
)
;
// Get the page scroll position
const
scrollY
=
await page.
evaluate
(
(
)
=>
{
return
window.
scrollY
;
}
)
;
}
)
;
Passing arguments to evaluate
page.evaluate() The second argument of evaluate is passed to the function inside the page.
Example
test
(
'Pass arguments to the page'
,
async
(
{
page
}
)
=>
{
await page.
goto
(
'https://www.runoob.com/'
)
;
// Pass a single argument
const
result1
=
await page.
evaluate
(
(
name
)
=>
{
return
`Hello
,
$
{
name
}
!
`
;
}
,
'RUNOOB'
)
;
// Pass an object argument
const
userInfo
=
{
name
:
'runoob'
,
role
:
'admin'
}
;
const
result2
=
await page.
evaluate
(
(
user
)
=>
{
return
`$
{
user.
name
}
role is $
{
user.
role
}
`
;
}
,
userInfo
)
;
// Pass multiple arguments (using destructuring)
const
result3
=
await page.
evaluate
(
(
{
x
,
y
}
)
=>
{
return
x
y
;
}
,
{
x
:
10
,
y
:
20
}
)
;
console.
log
(
'10 + 20 ='
,
result3
)
;
// 30
}
)
;
Passed arguments are JSON-serialized before being sent to the page context, so you cannot pass functions, DOM elements, or other non-serializable objects.
Returned values are also serialized.
locator.evaluate() element context execution
locator.evaluate() Execute code in the context of the matched element, with the argument containing a reference to the element's DOM node.
Example
test
(
'Get the DOM attributes of an element'
,
async
(
{
page
}
)
=>
{
await page.
goto
(
'https://www.runoob.com/'
)
;
// Get the textContent of a specific element
const
text
=
await page
.
getByRole
(
'heading'
,
{
name
:
'RUNOOB'
}
)
.
evaluate
(
node
=>
node.
textContent
)
;
// Get the style attributes of an element
const
color
=
await page
.
getByRole
(
'button'
,
{
name
:
'Search'
}
)
.
evaluate
(
node
=>
getComputedStyle
(
node
)
.
backgroundColor
)
;
}
)
;
locator.evaluateAll() iterate over all matched elements
evaluateAll() Execute the same code on all matched elements and return an array.
Example
test
(
'Get the text of all list items'
,
async
(
{
page
}
)
=>
{
await page.
goto
(
'https://www.runoob.com/'
)
;
// Get the text of all menu items
const
menuItems
=
await page
.
getByRole
(
'navigation'
)
.
locator
(
'a'
)
.
evaluateAll
(
links
=>
links.
map
(
link
=>
link.
textContent
)
)
;
console.
log
(
'Menu items:'
,
menuItems
)
;
// ['Home', 'Tutorials', 'Tools', ...]
}
)
;
page.addInitScript() inject init script
addInitScript() Inject JavaScript code before the page loads, allowing you to modify the browser environment before the page scripts run.
Example
test
(
'Inject init script'
,
async
(
{
page
}
)
=>
{
// Modify navigator properties before the page loads
await page.
addInitScript
(
(
)
=>
{
// Bypass WebDriver detection
Object
.
defineProperty
(
navigator
,
'webdriver'
,
{
get
:
(
)
=>
false
,
}
)
;
// Mock global variables on window
window.
RUNOOB_CONFIG
=
{
env
:
'test'
,
version
:
'1.0.0'
,
}
;
}
)
;
await page.
goto
(
'https://www.runoob.com/'
)
;
// Verify the injected variables take effect
const
config
=
await page.
evaluate
(
(
)
=>
window.
RUNOOB_CONFIG
)
;
console.
log
(
config
)
;
// { env: 'test', version: '1.0.0' }
}
)
;
addInitScript() Common uses:
| Purpose | Description |
|---|---|
| Mock browser APIs | Overwrite navigator.webdriver、navigator.language etc. |
| Inject custom configuration | Set test environment variables, mock data sources, etc. |
| Disable features | Disable Service Workers, disable logging, etc. |
| Polyfill | Add features that the page depends on but the test environment lacks |
Uses and limitations of ElementHandle
ElementHandle is a direct reference to a DOM element and was widely used in early versions of Playwright.
In most scenarios today, you should use Locator instead of ElementHandle.
Example
// ElementHandle approach (old style, not recommended)
const
handle
=
await page.$
(
'.my-element'
)
;
await handle.
click
(
)
;
// Locator approach (new style, recommended)
await page.
locator
(
'.my-element'
)
.
click
(
)
;
ElementHandle should only be used when you need to obtain boundingBox() or perform complex DOM operations.