ElementHandle.type() 方法
聚焦该元素,然后为文本中的每个字符发送 keydown、keypress/input 和 keyup 事件。
要按下特殊键(如 Control 或 ArrowDown),请使用 ElementHandle.press()。
签名
class ElementHandle {
type(text: string, options?: Readonly<KeyboardTypeOptions>): Promise<void>;
}
参数
|
参数 |
类型 |
说明 |
|---|---|---|
|
text |
string | |
|
options |
Readonly<KeyboardTypeOptions> |
(可选) 延迟毫秒数。默认为 0。 |
返回值:
Promise<void>
示例 1
await elementHandle.type('Hello'); // Types instantly
await elementHandle.type('World', {delay: 100}); // Types slower, like a user
示例 2
在文本字段中输入文本然后提交表单的示例:
const elementHandle = await page.$('input');
await elementHandle.type('some text');
await elementHandle.press('Enter');