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")
- worker.evaluate(expression, **kwargs)
- worker.evaluate_handle(expression, **kwargs)
- worker.url
worker.on("close")
Added in: v1.8- type: <Worker>
Emitted when this dedicated WebWorker is terminated.
worker.evaluate(expression, **kwargs)
Added in: v1.8expression
<str> JavaScript expression to be evaluated in the browser context. If the expresion evaluates to a function, the function is automatically invoked.#arg
<EvaluationArgument> Optional argument to pass toexpression
.#- 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)
Added in: v1.8expression
<str> JavaScript expression to be evaluated in the browser context. If the expresion evaluates to a function, the function is automatically invoked.#arg
<EvaluationArgument> Optional argument to pass toexpression
.#- 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.