Skip to main content
Version: 23.5.0

ElementHandle.type() 方法

¥ElementHandle.type() method

聚焦元素,然后为文本中的每个字符发送 keydownkeypress/inputkeyup 事件。

¥Focuses the element, and then sends a keydown, keypress/input, and keyup event for each character in the text.

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

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

签名

¥Signature

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

参数

¥Parameters

范围

类型

描述

text

string

options

只读<KeyboardTypeOptions>

(可选的)延迟以毫秒为单位。默认为 0。

Returns:

Promise<void>

示例 1

¥Example 1

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

示例 2

¥Example 2

在文本字段中输入内容然后提交表单的示例:

¥An example of typing into a text field and then submitting the form:

const elementHandle = await page.$('input');
await elementHandle.type('some text');
await elementHandle.press('Enter');