Skip to main content
Version: 23.2.0

Page.focus() 方法

¥Page.focus() method

此方法获取带有 selector 的元素并聚焦它。如果没有与 selector 匹配的元素,该方法将抛出错误。

¥This method fetches an element with selector and focuses it. If there's no element matching selector, the method throws an error.

签名

¥Signature

class Page {
focus(selector: string): Promise<void>;
}

参数

¥Parameters

范围

类型

描述

selector

string

要查询页面的 selectorCSS 选择器 可以按原样传递,Puppeteer 特定的选择器语法 允许通过 texta11y 角色和名称xpath跨影子根组合这些查询 进行查询。或者,你可以使用 prefix 指定选择器类型。如果有多个元素满足选择器,则第一个元素将获得焦点。

Returns:

Promise<void>

当元素匹配选择器成功聚焦时解析的 Promise。如果没有元素匹配选择器,则 promise 将被拒绝。

¥Promise which resolves when the element matching selector is successfully focused. The promise will be rejected if there is no element matching selector.

备注

¥Remarks

page.mainFrame().focus(selector) 的快捷方式。

¥Shortcut for page.mainFrame().focus(selector).