Skip to main content
Version: 23.2.0

Keyboard.type() 方法

¥Keyboard.type() method

为文本中的每个字符发送 keydownkeypress/inputkeyup 事件。

¥Sends a keydown, keypress/input, and keyup event for each character in the text.

签名

¥Signature

class Keyboard {
abstract type(
text: string,
options?: Readonly<KeyboardTypeOptions>
): Promise<void>;
}

参数

¥Parameters

范围

类型

描述

text

string

要输入到焦点元素中的文本。

options

只读<KeyboardTypeOptions>

(可选的)选项的对象。接受延迟,如果指定,则为 keydownkeyup 之间等待的时间(以毫秒为单位)。默认为 0。

Returns:

Promise<void>

备注

¥Remarks

要按特殊键,例如 ControlArrowDown,请使用 Keyboard.press()

¥To press a special key, like Control or ArrowDown, use Keyboard.press().

修改键不会影响 keyboard.type。按住 Shift 将不会键入大写文本。

¥Modifier keys DO NOT effect keyboard.type. Holding down Shift will not type the text in upper case.

示例

¥Example

await page.keyboard.type('Hello'); // Types instantly
await page.keyboard.type('World', {delay: 100}); // Types slower, like a user