Skip to content

Latest commit

 

History

History
288 lines (195 loc) · 10.1 KB

input.md

File metadata and controls

288 lines (195 loc) · 10.1 KB

Input

Contents


Text input

await page.fill('#name', 'Peter');

This is the easiest way to fill out the form fields. It focuses the element and triggers an input event with the entered text. It works for <input>, <textarea> and [contenteditable] elements.

Variations

// <input id=date type=date>
await page.fill('#date', '2020-02-02');

// <input id=date type=time>
await page.fill('#time', '13-15');

// <input id=local type=datetime-local>
await page.fill('#local', '2020-03-02T05:15');

API reference


Checkboxes

// <input id=agree type=checkbox></input>
await page.check('#agree');

// <label id=subscribe-label for=subscribe><input id=subscribe type=checkbox checked></input></label>
await page.uncheck('#subscribe-label');

This is the easiest way to check and uncheck a checkbox. This method can be used on the input[type=checkbox] and on the label associated with that input.

API reference


Select options

// <select id=colors>
//   <option value="red">Red</option>
//   <option value="green">Green</option>
//   <option value="blue">Blue</option>
// </select>

await page.selectOption('select#colors', 'green');

Selects one or multiple options in the <select> element. You can specify option value, label or elementHandle to select. Multiple options can be selected.

Variations

// Single selection matching the value
await page.selectOption('select#colors', 'blue');

// Single selection matching the label
await page.selectOption('select#colors', { label: 'Blue' });

// Multiple selected items
await page.selectOption('select#colors', ['red', 'green', 'blue']);

// Select the option element handle
const option = await page.$('#best-option');
await page.selectOption('select#colors', option);

API reference


Mouse click

// <button id=submit></button>

await page.click('button#submit');

Performs a simple human click. Under the hood, this and other pointer-related methods:

  • wait for element with given selector to be in DOM
  • wait for it to become displayed, i.e. not empty, no display:none, no visibility:hidden
  • wait for it to stop moving, for example, until css transition finishes
  • scroll the element into view
  • wait for it to receive pointer events at the action point, for example, waits until element becomes non-obscured by other elements
  • retry if the element is detached during any of the above checks

Variations

// Double click element
await page.dblclick('#item');

// Right click element
await page.click('#item', { button: 'right' });

// Shift click element
await page.click('#item', { modifiers: ['Shift'] });

// Hover over element without clicking
await page.hover('#item');

// Click the top left corner of the element
await page.click('#item', { position: { x: 0, y: 0} });

API reference


Type characters

// <textarea id=area></textarea>

await page.type('#area', 'Hello World!');

Note that most of the time, page.fill will just work. You only need to type characters if there is special keyboard handling on the page.

But sometimes it is important to type into the field character by character, as if it was a user with a real keyboard. This method will emit all the necessary keyboard events, with all the keydown, keyup, keypress events in place. You can even specify the optional delay between the key presses to simulate real user behavior.

API reference


Keys and shortcuts

// <button id=submit></button>
await page.press('#submit', 'Enter');

// <input id=name></input>
await page.press('#name', 'Control+ArrowRight');

// <input id=value></input>
await page.press('#value', '$');

This method focuses the selected element and produces a single keystroke. It accepts the logical key names that are emitted in the keyboardEvent.key property of the keyboard events:

Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape,
ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrayRight,
ArrowUp, F1 - F12, Digit0 - Digit9, KeyA - KeyZ, etc.
  • You can alternatively specify a single character you'd like to produce such as "a" or "#".

  • Following modification shortcuts are also supported: Shift, Control, Alt, Meta.

Variations

// <input id=name></input>
await page.press('#name', '$');

Simple version produces a single character. This character is case-sensitive, so "a" and "A" will produce different results.

// <input id=name></input>
await page.press('#name', 'Shift+A');

// <input id=name></input>
await page.press('#name', 'Shift+ArrowLeft');

Shortcuts such as "Control+o" or "Control+Shift+T" are supported as well. When speficied with the modifier, modifier is pressed and being held while the subsequent key is being pressed.

Note that you still need to specify the capital A in Shift-A to produce the capital character. Shift-a produces a lower-case one as if you had the CapsLock toggled.

API reference


Upload files

// <input id=upload type=file>

await page.setInputFiles('input#upload', 'myfile.pdf');

You can select input files for upload using the page.setInputFiles method. It expects first argument to point to an input element with the type "file". Multiple files can be passed in the array. If some of the file paths are relative, they are resolved relative to the current working directory. Empty array clears the selected files.

Variations

// Select multiple files.
await page.setInputFiles('input#upload', ['file1.txt', 'file2.txt']);

// Upload buffer from memory, without reading from file.
await page.setInputFiles('input#upload', {
	name: 'file.txt',
	mimeType: 'text/plain',
	buffer: Buffer.from('this is test')
});

// Remove all the selected files
await page.setInputFiles('input#upload', []);

API reference


Focus element

// <input id=name>

await page.focus('input#name');

For the dynamic pages that handle focus events, you can focus the given element.

API reference