🖱️ User Input & Click Actionability
Interacting with web elements in Playwright is governed by strict actionability checks. Before triggering any click or input action, Playwright automatically ensures the target element is attached to the DOM, visible in the viewport, stable (not animating), enabled (not disabled), and capable of receiving pointer events without being occluded by overlays. Rather than writing brittle JavaScript click dispatches, Playwright simulates real OS-level input hardware events, distinguishing between instant value population (fill()) and authentic user typing (press_sequentially()).
click() or fill(), Playwright automatically performs non-blocking auto-waiting checks to verify that the element is ready for user interaction.
[ Action Triggered: locator.click() / locator.fill() ]
│
▼
1. Attached? ──► Element must exist within the active DOM tree.
│
▼
2. Visible? ──► Has non-empty bounding box, not display:none / visibility:hidden.
│
▼
3. Stable? ──► CSS transitions and scroll animations have stopped moving.
│
▼
4. Enabled? ──► Not marked with 'disabled' attribute.
│
▼
5. Editable? ──► For inputs: must not have 'readonly' attribute.
│
▼
6. Receives Events? ──► Element is at the top of the hit test (not occluded by modals).
locator.fill() is the recommended standard for web forms. It clears existing text automatically, focuses the element, inputs the value instantly, and triggers input and change events.
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.set_content("""
""")
name_field = page.get_by_label("Company Legal Name")
# 1. fill() automatically clears previous text before entering new value
name_field.fill("Nikita Infotech Systems")
expect(name_field).to_have_value("Nikita Infotech Systems")
# 2. Explicit clear() empties the input field
name_field.clear()
expect(name_field).to_be_empty()
# 3. Refill with fresh test data
name_field.fill("Nikita Software & Engineering")
expect(name_field).to_have_value("Nikita Software & Engineering")
print(f"Verified Final Input Value: '{name_field.input_value()}'")
browser.close()
keydown/keyup events, use press_sequentially() with a simulated inter-key delay.
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
# Dynamic search input that listens to individual keystroke events
page.set_content("""
""")
search_input = page.get_by_placeholder("Search components...")
# Focus and type character-by-character with a 50ms delay between keys
search_input.press_sequentially("Playwright Locators", delay=50)
results_div = page.locator("#results")
expect(results_div).to_have_text("Searching query: Playwright Locators")
print(f"Simulated Human Typing Verified: '{results_div.inner_text()}'")
browser.close()
dblclick() for editable inline text and click(button="right") for custom context menus.
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.set_content("""
project_spec.pdf
No action
""")
file_card = page.locator("#file-item")
log_display = page.locator("#action-log")
# 1. Double Click action
file_card.dblclick()
expect(log_display).to_have_text("FILE_OPENED")
print(f"Double-click triggered state: '{log_display.inner_text()}'")
# 2. Right Click (Context Menu trigger)
file_card.click(button="right")
expect(log_display).to_have_text("CONTEXT_MENU_OPENED")
print(f"Right-click triggered state: '{log_display.inner_text()}'")
browser.close()
click(modifiers=[...]).
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.set_content("""
Document 1
Document 2
None
""")
item2 = page.locator("#i2")
status_box = page.locator("#status")
# Dispatch click while holding down the Shift key
item2.click(modifiers=["Shift"])
expect(status_box).to_have_text("MULTI_SELECT_SHIFT_ACTIVE")
print("Modifier key action verified: Shift + Click dispatched successfully!")
browser.close()
Enter, Escape, Tab, Control+A, or Backspace) directly to focused locators using locator.press().
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://demo.playwright.dev/todomvc/")
input_field = page.get_by_placeholder("What needs to be done?")
# 1. Fill initial text
input_field.fill("Temporary task item")
# 2. Select entire text string via Control+A / Meta+A shortcut
input_field.press("ControlOrMeta+A")
# 3. Delete selected text via Backspace key
input_field.press("Backspace")
expect(input_field).to_be_empty()
# 4. Enter new text and submit using the Enter key
input_field.fill("Finalized Production Task")
input_field.press("Enter")
# Verify item was added to the list
todo_item = page.locator(".todo-list li").first
expect(todo_item).to_have_text("Finalized Production Task")
print(f"Submitted Task via Enter key: '{todo_item.inner_text()}'")
browser.close()
force=True to bypass actionability checks and trigger the click directly.
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
# Page where a transparent overlay div occludes the underlying button
page.set_content("""
Not Clicked
""")
target_button = page.locator("#target-btn")
output_div = page.locator("#out")
# Standard click would wait and timeout due to the occluding overlay
# Using force=True dispatches the pointer event directly onto the element
target_button.click(force=True)
expect(output_div).to_have_text("CLICKED_SUCCESS")
print(f"Forced click dispatched through overlay: '{output_div.inner_text()}'")
browser.close()
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.set_content("""
X: 0
""")
bar = page.locator("#progress-bar")
pos_output = page.locator("#click-pos")
# Click at exactly 25% across the width (X=50, Y=15)
bar.click(position={"x": 50, "y": 15})
expect(pos_output).to_have_text("X: 50")
print(f"Targeted Coordinate Click 1: '{pos_output.inner_text()}'")
# Click at 75% across the width (X=150, Y=15)
bar.click(position={"x": 150, "y": 15})
expect(pos_output).to_have_text("X: 150")
print(f"Targeted Coordinate Click 2: '{pos_output.inner_text()}'")
browser.close()
from playwright.sync_api import sync_playwright, expect
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.set_content("""
""")
notes_field = page.get_by_label("Release Notes Description")
# Multiline text block with newline characters
multiline_text = (
"Version 2.4.0 Release Changelog:\n"
"- Enhanced auto-waiting assertion pipelines\n"
"- Integrated multi-browser launch options\n"
"- Optimized headless execution parameters"
)
notes_field.fill(multiline_text)
expect(notes_field).to_have_value(multiline_text)
captured_val = notes_field.input_value()
print("Multi-line textarea value captured successfully:")
print(" Lines Count:", len(captured_val.splitlines()))
print(" First Line :", captured_val.splitlines()[0])
browser.close()
PLAYWRIGHT INPUTS & CLICKS METHODS MATRIX:
┌──────────────────────────────┬──────────────────────────────┬─────────────────────────────────────────────────────────┐
│ Method │ Key Parameters │ Primary Description / Use Case │
├──────────────────────────────┼──────────────────────────────┼─────────────────────────────────────────────────────────┤
│ locator.fill(value) │ value: str, timeout, force │ Clears existing text and inputs string instantly. │
│ locator.clear() │ timeout, force │ Clears all text from an input or textarea element. │
│ locator.press_sequentially() │ text: str, delay: ms │ Simulates real typing, sending keydown/keyup per char. │
│ locator.click() │ button, modifiers, position │ Standard single pointer click on element center. │
│ locator.dblclick() │ button, modifiers, delay │ Dispatches double click (useful for inline text edit). │
│ locator.press(key) │ key: str ("Enter", "Tab") │ Sends specific keyboard shortcut to the focused element.│
│ locator.check() │ position, timeout, force │ Checks checkbox or radio button if not already checked. │
│ locator.uncheck() │ position, timeout, force │ Unchecks checkbox if currently checked. │
│ locator.focus() │ timeout │ Focuses target element directly. │
│ locator.hover() │ position, modifiers │ Moves mouse pointer over element to trigger hover CSS. │
├──────────────────────────────┴──────────────────────────────┴─────────────────────────────────────────────────────────┤
│ COMMON CLICK PARAMETERS: │
│ * button="left"|"right"|"middle" ──► Selects which mouse pointer button to click. │
│ * modifiers=["Shift"|"Control"|"Alt"|"Meta"] ──► Holds modifier keys down during the click action. │
│ * position={"x": int, "y": int} ──► Relative offset coordinates inside element bounding box. │
│ * force=True ──► Bypasses actionability checks and pointer hit-test verification. │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘