Manuals / Playwright with Python / Chapter 12
Part 2 · Core Interactions · beginner · ~45 min · Chapter 12 of 61
6. Actions
page.get_by_role("button", name="Submit").click() page.locator(".card").dblclick() # fill — clears the field first, then sets the value (fast, recommended default) page.get_by_label("Username").fill("simran") # type — types character by character, triggering keydown/keyup events for each page.get_by_label("Search").type("laptop", delay=100) # press — sends a single keyboard key page.get_by_label("
Step 1 of 5
Overview
# fill — clears the field first, then sets the value (fast, recommended default) # type — types character by character, triggering keydown/keyup events for each # press — sends a single keyboard key The fill vs type distinction is a common point of confusion: fill is much faster and correct a field has a live autocomplete or character-count validator that listens to individual keydown events, fill might not trigger it correctly. Reach for type specifically in that situation, not by default. .click() / .dblclick() What it does: Simulates a mouse click / double-click on the located element. Types/params: Pointers: Auto-waits for actionability first unless force=True. Avoid forcing unless you're certain the "not actionable" state is a false positive. .fill(value) What it does: Clears the field and sets its value directly (not via simulated keystrokes). Types/params: Pointers: Fast and the right default for most form-filling. Won't trigger per-keystroke JS listeners — use .type() for those cases instead. .type(text, delay=...) What it does: Types text character-by-character, firing real keydown/keyup events for each character. Types/params: (e.g. 100) for debounced/live-search inputs Pointers: Slower than .fill() — reserve it specifically for inputs that need real keystroke events (autocomplete, input masks, character counters). .press(key) What it does: Sends a single keyboard key or key combination. Types/params: Pointers: Useful for submitting forms via Enter or triggering keyboard shortcuts without a visible button to click. nothing (no error), unlike calling .click() on a checkbox, which would toggle it. .check() / .uncheck() What it does: Sets a checkbox/radio to checked/unchecked state. Types/params: Pointers: Idempotent — prefer these over .click() for checkboxes when you want a guaranteed end state regardless of current state. Note: select_option only works on native HTML <select> elements. Many modern UIs use custom-built dropdowns (a <div> styled to look like a select) — for those, treat it like any other clickable element: click to open, then click the desired option as a regular list item. .select_option(value=... / label=... / [list]) What it does: Selects one or more options in a native <select> dropdown. Types/params: Pointers: Only works on native <select>. Custom JS-built dropdowns need click-to-open-then-click-option handling instead. Hover is commonly needed just to reveal an element before you can interact with what it reveals. Drag-and-drop via .drag_to() handles the full mousedown → mousemove → mouseup sequence internally — for especially custom drag implementations, you may need to fall back to manual mouse events. .hover() What it does: Moves the mouse over the element without clicking. Types/params: Pointers: Commonly a precursor step to reveal hover-triggered menus/tooltips before you can act on what appears. .drag_to(target_locator) What it does: Performs a full drag-and-drop from the source element to a target locator. Types/params: Pointers: Handles standard HTML5 drag-and-drop well. Custom JS drag libraries with unusual event expectations may need manual page.mouse sequences instead.
- button (string, default "left")
- "left", "right", "middle" — which mouse button to simulate
- click_count (integer, default 1 for click)
- Number of clicks in the sequence (rarely changed manually)
- delay (number, ms, optional)
- Delay between mousedown and mouseup — useful for UI that distinguishes click duration
- modifiers (list of strings, optional)
- e.g. ["Shift"], ["Control", "Alt"] — held during the click, for shift-click/ctrl-click behavior
- force (boolean, default False)
- True → skips actionability checks and clicks regardless
- False → normal safe behavior (recommended default)
- value (string, required) — the exact text to set as the input's value
- text (string, required) — the text to type out, one character at a time
- delay (number, ms, optional, default 0) — pause between each keystroke, set higher
- key (string, required)
- Single key: "Enter", "Tab", "Escape", "ArrowDown"
- Combination: "Control+A", "Shift+Tab" (joined with +)
- force (boolean, default False)
- True → skips actionability checks
- False → normal safe behavior
Example
page.get_by_label("Remember me").check()
page.get_by_label("Subscribe to newsletter").uncheck()
# is_checked() for asserting current state
assert page.get_by_label("Remember me").is_checked()
check()/uncheck() are idempotent — calling check() on an already-checked box does
# By visible label
page.get_by_label("Country").select_option(label="Nepal")
# By value attribute
page.get_by_label("Country").select_option(value="NP")
# Multi-select
page.get_by_label("Skills").select_option(["Python", "Playwright"])
page.get_by_text("Account menu").hover() # reveals a dropdown menu, for example
page.locator("#source-item").drag_to(page.locator("#drop-zone"))Chapter learning outcomes
- page.get_by_role("button", name="Submit").click() page.locator(".card").dblclick
Clear these before you leave
Side quest
6. Actions deliverable
Apply one idea from “Overview” in a small script or note.