Skip to main content

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.

page.onWorker(worker -> {
System.out.println("Worker created: " + worker.url());
worker.onClose(worker1 -> System.out.println("Worker destroyed: " + worker1.url()));
});
System.out.println("Current workers:");
for (Worker worker : page.workers())
System.out.println(" " + worker.url());

方法

🌐 Methods

evaluate

Added before v1.9 worker.evaluate

返回 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() 的函数返回一个非 Serializable 值,那么 Worker.evaluate() 将返回 undefined。Playwright 还支持传递一些 JSON 无法序列化的其他值:-0NaNInfinity-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.

用法

Worker.evaluate(expression);
Worker.evaluate(expression, arg);

参数

  • expression String#

    将在浏览器上下文中求值的 JavaScript 表达式。如果表达式求值为一个函数,该函数将被自动调用。

  • arg EvaluationArgument (optional)#

    可选参数,传递给 expression

返回


evaluateHandle

Added before v1.9 worker.evaluateHandle

expression 的返回值作为 JSHandle 返回。

🌐 Returns the return value of expression as a JSHandle.

Worker.evaluate()Worker.evaluateHandle() 唯一的区别是 Worker.evaluateHandle() 会返回 JSHandle

🌐 The only difference between Worker.evaluate() and Worker.evaluateHandle() is that Worker.evaluateHandle() returns JSHandle.

如果传递给 Worker.evaluateHandle() 的函数返回一个 Promise,那么 Worker.evaluateHandle() 会等待该 promise 解决并返回其值。

🌐 If the function passed to the Worker.evaluateHandle() returns a Promise, then Worker.evaluateHandle() would wait for the promise to resolve and return its value.

用法

Worker.evaluateHandle(expression);
Worker.evaluateHandle(expression, arg);

参数

  • expression String#

    将在浏览器上下文中求值的 JavaScript 表达式。如果表达式求值为一个函数,该函数将被自动调用。

  • arg EvaluationArgument (optional)#

    可选参数,传递给 expression

返回


url

Added before v1.9 worker.url

用法

Worker.url();

返回


waitForClose

Added in: v1.10 worker.waitForClose

执行操作并等待 Worker 关闭。

🌐 Performs action and waits for the Worker to close.

用法

Worker.waitForClose(callback);
Worker.waitForClose(callback, options);

参数

  • options Worker.WaitForCloseOptions (optional)

    • setTimeout double (optional) Added in: v1.9#

      等待的最长时间(以毫秒为单位)。默认为 30000(30 秒)。传入 0 可禁用超时。可以使用 BrowserContext.setDefaultTimeout() 来更改默认值。

  • callback Runnable Added in: v1.9#

    执行触发事件的操作的回调。

返回


waitForConsoleMessage

Added in: v1.57 worker.waitForConsoleMessage

执行操作并等待控制台消息。

🌐 Performs action and waits for a console message.

用法

Worker.waitForConsoleMessage(callback);
Worker.waitForConsoleMessage(callback, options);

参数

  • options Worker.WaitForConsoleMessageOptions (optional)

  • callback Runnable#

    执行触发事件的操作的回调。

返回


事件

🌐 Events

onClose(handler)

Added before v1.9 worker.onClose(handler)

当这个专用的 WebWorker 被终止时触发。

🌐 Emitted when this dedicated WebWorker is terminated.

用法

Worker.onClose(handler)

事件数据


onConsole(handler)

Added in: v1.57 worker.onConsole(handler)

当 worker 中的 JavaScript 调用某个 console API 方法时触发,例如 console.logconsole.dir

🌐 Emitted when JavaScript within the worker calls one of console API methods, e.g. console.log or console.dir.

用法

Worker.onConsole(handler)

事件数据