Skip to main content

对话框

🌐 Dialogs

处理阻止页面交互的浏览器对话框(警告、确认、提示)。

🌐 Handle browser dialogs (alert, confirm, prompt) that block page interaction.

命令

🌐 Commands

命令描述
dialog-accept [prompt]接受对话框,可选择提供提示文本
dialog-dismiss关闭(取消)对话框

对话是如何被报道的

🌐 How a dialog is reported

当对话框打开时,每个命令都会在 Modal state 部分下报告它,并告诉你哪个命令可以清除它:

🌐 While a dialog is open, every command reports it under a Modal state section and tells you which command clears it:


### Modal state

- ["confirm" dialog with message "Are you sure you want to delete this?"]: can be handled by dialog-accept or dialog-dismiss

在处理好之前,其他事情都无法继续。

🌐 Nothing else can proceed until it is handled.

警告对话框

🌐 Alert dialogs

$ playwright-cli click e5

### Modal state

- ["alert" dialog with message "Item has been deleted."]: can be handled by dialog-accept or dialog-dismiss

$ playwright-cli dialog-accept

确认对话框

🌐 Confirm dialogs

$ playwright-cli click e10

### Modal state

- ["confirm" dialog with message "Are you sure you want to delete this?"]: can be handled by dialog-accept or dialog-dismiss

# Accept (OK)
$ playwright-cli dialog-accept

# Or dismiss (Cancel)
$ playwright-cli dialog-dismiss

提示对话框

🌐 Prompt dialogs

$ playwright-cli click e8

### Modal state

- ["prompt" dialog with message "Enter your name:"]: can be handled by dialog-accept or dialog-dismiss

# Accept with text
$ playwright-cli dialog-accept "Alice"

# Or dismiss (cancels the prompt)
$ playwright-cli dialog-dismiss

文件选择器

🌐 File choosers

文件选择器的报告方式相同,并且使用upload而不是对话框命令来清除:

🌐 A file chooser is reported the same way, and is cleared with upload rather than the dialog commands:


### Modal state

- [File chooser]: can be handled by upload
playwright-cli upload /abs/path/to/document.pdf

工作流程

🌐 Workflow

$ playwright-cli click e12

### Modal state

- ["confirm" dialog with message "Discard unsaved changes?"]: can be handled by dialog-accept or dialog-dismiss

$ playwright-cli dialog-accept

$ playwright-cli snapshot
# Page now shows updated state after dialog was accepted