Worker
Added in: v1.8The Worker class represents a WebWorker. worker event is emitted on the page object to signal a worker creation. close event is emitted on the worker object when the worker is gone.
def handle_worker(worker):
print("worker created: " + worker.url)
worker.on("close", lambda: print("worker destroyed: " + worker.url))
page.on('worker', handle_worker)
print("current workers:")
for worker in page.workers:
print(" " + worker.url)Methods
worker.evaluate()
Added in: v1.8Returns the return value of expression.
If the function passed to the worker.evaluate() returns a [Promise], then worker.evaluate() would wait for the promise to resolve and return its value.
If the function passed to the worker.evaluate() returns a non-[Serializable] value, then worker.evaluate() returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.
Arguments
JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
argEvaluationArgument(optional)Optional argument to pass to
expression.
Returns
Serializable
worker.evaluate_handle()
Added in: v1.8Returns the return value of expression as a JSHandle.
The only difference between worker.evaluate() and worker.evaluate_handle() is that worker.evaluate_handle() returns JSHandle.
If the function passed to the worker.evaluate_handle() returns a [Promise], then worker.evaluate_handle() would wait for the promise to resolve and return its value.
Arguments
JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
argEvaluationArgument(optional)Optional argument to pass to
expression.
Returns
JSHandle
worker.expect_event()
Added in: v1.57Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the page is closed before the event is fired. Returns the event data value.
Usage
async with worker.expect_event("console") as event_info:
await worker.evaluate("console.log(42)")
message = await event_info.valuewith worker.expect_event("console") as event_info:
worker.evaluate("console.log(42)")
message = event_info.valueArguments
Event name, same one typically passed into
*.on(event).predicatefunction()(optional)Receives the event data and resolves to truthy value when the waiting should resolve.
Maximum time to wait for in milliseconds. Defaults to
30000(30 seconds). Pass0to disable timeout. The default value can be changed by using thebrowser_context.set_default_timeout().
Returns
EventContextManager
worker.url()
Added in: v1.8Returns
string
Events
worker.on("close")
Added in: v1.8Emitted when this dedicated WebWorker is terminated.
Event data
Worker
worker.on("console")
Added in: v1.57Emitted when JavaScript within the worker calls one of console API methods, e.g. console.log or console.dir.
Event data
ConsoleMessage