Skip to content

Playwright (Python)

Overview

The playwright-python plugin generates Python code using the Playwright library for browser automation and testing.

within Browser (plugin: playwright-python, type: browser)
go to "https://example.com"

Types

browser

The main browser context. Launches a Chromium browser and creates a new page.

Type Modifiers:

ModifierTypeDescription
urlquotedInitial URL to navigate to on launch
headlessquotedRun browser in headless mode

Patterns:

PatternDescription
go to <url>Navigate to URL
click <target>Click element by selector
type <text> into <target>Type text into input element
see a/an/any/no <target>Assert element visibility or count
wait for <target>Wait for element to appear
expect failureExpect code block to raise an exception

Pattern Modifiers:

PatternModifierTypeDescription
go totimeoutquotedNavigation timeout
clicktimeoutquotedClick timeout
typetimeoutquotedType timeout
seetextquotedText content to match
wait fortimeoutquotedWait timeout
wait forstatequotedWait state (e.g., “visible”, “attached”)
expect failureerrorquotedExpected exception type
expect failurecapturequotedVariable name to capture exception

element

A scoped element context for targeting nested elements. Inherits patterns from browser type.

Type Modifiers:

ModifierTypeDescription
idquotedSelect by element ID
classquotedSelect by CSS class
testidquotedSelect by data-testid attribute
labelquotedSelect by accessible label
within Login Form (type: element, testid: "login-form")
type "user@example.com" into "Email"

Patterns:

The element type supports the same patterns as browser: click, type, see, wait for, and expect failure.

failure_block

A passthrough block used inside expect failure to contain actions that should raise an exception.

Pattern Details

see

The see pattern requires a quantifier to specify the expected element count:

QuantifierAssertion
a or anExactly one element exists
anyAt least one element is visible
noZero elements exist
see a "Submit Button"
see no "Error Message"
see any "Product Card"

expect failure

Wrap actions that should raise an exception. Useful for testing error handling.

expect failure (error: "TimeoutError", capture: "exc_info")
click "Nonexistent Element"

Generated code uses pytest.raises context manager.

Example

vanya 0.1
"Complete checkout flow"
within Browser (plugin: playwright-python, type: browser, headless: "true")
go to "https://shop.example.com"
click "Add to Cart"
click "Checkout"
within Shipping Form (type: element, testid: "shipping-form")
type "123 Main St" into "Address"
type "10001" into "ZIP"
click "Place Order"
see a "Order Confirmation"
see no "Error Message"

Generated Code

The above example generates Python code similar to:

ctx = playwright.chromium.launch().new_page()
ctx.goto("https://shop.example.com")
ctx.locator("Add to Cart").click()
ctx.locator("Checkout").click()
ctx = parent.locator("[data-testid='shipping-form']")
ctx.locator("Address").fill("123 Main St")
ctx.locator("ZIP").fill("10001")
ctx.locator("Place Order").click()
expect(ctx.locator("Order Confirmation")).to_have_count(1)
expect(ctx.locator("Error Message")).to_have_count(0)