Worker
Worker 类代表 WebWorker。worker
事件在页面对象上触发,以指示工作线程创建。当工作线程消失时,工作线程对象上会触发 close
事件。
¥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)
方法
¥Methods
evaluate
Added before v1.9返回 expression 的返回值。
¥Returns the return value of expression.
如果传递给 worker.evaluate() 的函数返回 Promise,那么 worker.evaluate() 将等待 Promise 解析并返回其值。
¥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.
如果传递给 worker.evaluate() 的函数返回非 可串行化 值,则 worker.evaluate() 返回 undefined
。Playwright 还支持传输一些 JSON
无法序列化的附加值:-0
、NaN
、Infinity
、-Infinity
。
¥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
.
用法
¥Usage
worker.evaluate(expression)
worker.evaluate(expression, **kwargs)
参数
¥Arguments
要在浏览器上下文中执行的 JavaScript 表达式。如果表达式的计算结果为函数,则会自动调用该函数。
¥JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
arg
EvaluationArgument (optional)#
传递给 expression 的可选参数。
¥Optional argument to pass to expression.
返回
¥Returns
evaluate_handle
Added before v1.9将 expression 的返回值作为 JSHandle 返回。
¥Returns the return value of expression as a JSHandle.
worker.evaluate() 和 worker.evaluate_handle() 之间的唯一区别是 worker.evaluate_handle() 返回 JSHandle。
¥The only difference between worker.evaluate() and worker.evaluate_handle() is that worker.evaluate_handle() returns JSHandle.
如果传递给 worker.evaluate_handle() 的函数返回 Promise,那么 worker.evaluate_handle() 将等待 Promise 解析并返回其值。
¥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.
用法
¥Usage
worker.evaluate_handle(expression)
worker.evaluate_handle(expression, **kwargs)
参数
¥Arguments
要在浏览器上下文中执行的 JavaScript 表达式。如果表达式的计算结果为函数,则会自动调用该函数。
¥JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.
arg
EvaluationArgument (optional)#
传递给 expression 的可选参数。
¥Optional argument to pass to expression.
返回
¥Returns
属性
¥Properties
url
Added before v1.9用法
¥Usage
worker.url
返回
¥Returns
事件
¥Events
on("close")
Added before v1.9当该专用 WebWorker 终止时触发。
¥Emitted when this dedicated WebWorker is terminated.
用法
¥Usage
worker.on("close", handler)
事件数据
¥Event data