Version: Next

Worker

The 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)

worker.on("close")#

Emitted when this dedicated WebWorker is terminated.

worker.evaluate(expression, **kwargs)#

  • expression <str> JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted as a function. Otherwise, evaluated as an expression.
  • arg <[EvaluationArgument]> Optional argument to pass to expression.
  • returns: <Serializable>

Returns the return value of expression.

If the function passed to the worker.evaluate(expression, **kwargs) returns a Promise, then worker.evaluate(expression, **kwargs) would wait for the promise to resolve and return its value.

If the function passed to the worker.evaluate(expression, **kwargs) returns a non-Serializable value, then worker.evaluate(expression, **kwargs) returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.

worker.evaluate_handle(expression, **kwargs)#

  • expression <str> JavaScript expression to be evaluated in the browser context. If it looks like a function declaration, it is interpreted as a function. Otherwise, evaluated as an expression.
  • arg <[EvaluationArgument]> Optional argument to pass to expression.
  • returns: <JSHandle>

Returns the return value of expression as a JSHandle.

The only difference between worker.evaluate(expression, **kwargs) and worker.evaluate_handle(expression, **kwargs) is that worker.evaluate_handle(expression, **kwargs) returns JSHandle.

If the function passed to the worker.evaluate_handle(expression, **kwargs) returns a Promise, then worker.evaluate_handle(expression, **kwargs) would wait for the promise to resolve and return its value.

worker.url#