ElementHandle.type() 方法
將焦點放在元素上,然後為文字中的每個字元發送 keydown
、keypress
/input
和 keyup
事件。
要按下特殊的鍵,例如 Control
或 ArrowDown
,請使用 ElementHandle.press()。
簽名
class ElementHandle {
type(text: string, options?: Readonly<KeyboardTypeOptions>): Promise<void>;
}
參數
參數 | 類型 | 說明 |
---|---|---|
text | 字串 | |
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');