Skip to main content

互动

🌐 Interaction

命令

🌐 Commands

命令描述
click <ref> [button]单击元素(左键、右键或中键)
dblclick <ref> [button]双击元素
fill <ref> <text>清除并填写输入框文本
fill <ref> <text> --submit填写并按回车
type <text>在聚焦元素中输入文本
select <ref> <value>选择下拉列表选项
check <ref>勾选复选框或单选按钮
uncheck <ref>取消勾选复选框
hover <ref>悬停在元素上
drag <startRef> <endRef>拖放
upload <file>上传文件
resize <width> <height>调整浏览器窗口大小

目标元素

🌐 Targeting elements

🌐 Refs from snapshots (recommended)

playwright-cli snapshot                 # get element refs
playwright-cli click e15 # click by ref
playwright-cli fill e3 "hello" # fill by ref

CSS 选择器

🌐 CSS selectors

playwright-cli click "#main > button.submit"
playwright-cli click "[data-testid='submit']"
playwright-cli fill "#email" "test@example.com"

Playwright 定位器

🌐 Playwright locators

playwright-cli click "getByRole('button', { name: 'Submit' })"
playwright-cli click "getByTestId('submit-button')"
playwright-cli click "getByText('Login')"
playwright-cli fill "getByLabel('Email')" "test@example.com"

表单交互

🌐 Form interaction

# Fill a text field
playwright-cli fill e3 "test@example.com"

# Fill and submit (presses Enter)
playwright-cli fill e3 "search query" --submit

# Select from dropdown
playwright-cli select e7 "United States"

# Checkboxes
playwright-cli check e10 # check
playwright-cli uncheck e10 # uncheck

# File upload
playwright-cli upload /path/to/file.pdf

# Resize browser window
playwright-cli resize 375 812 # mobile viewport
playwright-cli resize 1920 1080 # desktop

工作流程:登录表单

🌐 Workflow: login form

$ playwright-cli snapshot
# - textbox "Email" [ref=e3]
# - textbox "Password" [ref=e5]
# - checkbox "Remember me" [ref=e7]
# - button "Sign in" [ref=e9]

$ playwright-cli fill e3 "alice@example.com"
$ playwright-cli fill e5 "s3cureP@ss!"
$ playwright-cli check e7
$ playwright-cli click e9

$ playwright-cli snapshot
# - heading "Welcome back, Alice!" [level=1]