Input

Text input

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.

  1. // Text input
  2. await page.fill('#name', 'Peter');
  3. // Date input
  4. await page.fill('#date', '2020-02-02');
  5. // Time input
  6. await page.fill('#time', '13-15');
  7. // Local datetime input
  8. await page.fill('#local', '2020-03-02T05:15');

API reference

Checkboxes

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.

  1. // Check the checkbox
  2. await page.check('#agree');
  3. // Uncheck by input <label>.
  4. await page.uncheck('#subscribe-label');

API reference

Select options

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

  1. // Single selection matching the value
  2. await page.selectOption('select#colors', 'blue');
  3. // Single selection matching the label
  4. await page.selectOption('select#colors', { label: 'Blue' });
  5. // Multiple selected items
  6. await page.selectOption('select#colors', ['red', 'green', 'blue']);
  7. // Select the option via element handle
  8. const option = await page.$('#best-option');
  9. await page.selectOption('select#colors', option);

API reference

Mouse click

Performs a simple human click.

  1. // Generic click
  2. await page.click('button#submit');
  3. // Double click
  4. await page.dblclick('#item');
  5. // Right click
  6. await page.click('#item', { button: 'right' });
  7. // Shift + click
  8. await page.click('#item', { modifiers: ['Shift'] });
  9. // Hover over element
  10. await page.hover('#item');
  11. // Click the top left corner
  12. await page.click('#item', { position: { x: 0, y: 0} });

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

Forcing the click

Sometimes, apps use non-trivial logic where hovering the element overlays it with another element that intercepts the click. This behavior is indistinguishable from a bug where element gets covered and the click is dispatched elsewhere. If you know this is taking place, you can bypass the actionability checks and force the click:

  1. await page.click('button#submit', { force: true });

Programmatic click

If you are not interested in testing your app under the real conditions and want to simulate the click by any means possible, you can trigger the HTMLElement.click() behavior via simply dispatching a click event on the element:

  1. await page.dispatchEvent('button#submit', 'click');

API reference

Type characters

Type into the field character by character, as if it was a user with a real keyboard.

  1. // Type character by character
  2. await page.type('#area', 'Hello World!');

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.

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.

API reference

Keys and shortcuts

  1. // Hit Enter
  2. await page.press('#submit', 'Enter');
  3. // Dispatch Control+Right
  4. await page.press('#name', 'Control+ArrowRight');
  5. // Press $ sign on keyboard
  6. 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:

  1. Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape,
  2. ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrayRight,
  3. 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.

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

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

Shortcuts such as "Control+o" or "Control+Shift+T" are supported as well. When specified 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

  1. // Select one file
  2. await page.setInputFiles('input#upload', 'myfile.pdf');
  3. // Select multiple files
  4. await page.setInputFiles('input#upload', ['file1.txt', 'file2.txt']);
  5. // Remove all the selected files
  6. await page.setInputFiles('input#upload', []);
  7. // Upload buffer from memory
  8. await page.setInputFiles('input#upload', {
  9. name: 'file.txt',
  10. mimeType: 'text/plain',
  11. buffer: Buffer.from('this is test')
  12. });

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.

API reference

Focus element

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

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

API reference