Dialog
[对话] 对象通过页面的 Page.onDialog(handler) 事件分发。
使用 Dialog 类的示例:
🌐 An example of using Dialog class:
import com.microsoft.playwright.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
Browser browser = chromium.launch();
Page page = browser.newPage();
page.onDialog(dialog -> {
System.out.println(dialog.message());
dialog.dismiss();
});
page.evaluate("alert('1')");
browser.close();
}
}
}
note
对话框会自动关闭,除非有一个 Page.onDialog(handler) 监听器。当监听器存在时,必须要么 Dialog.accept() 要么 Dialog.dismiss() 对话框——否则页面会因为等待对话框而 冻结,点击等操作将永远无法完成。
方法
🌐 Methods
accept
Added before v1.9当对话框被接受时返回。
🌐 Returns when the dialog has been accepted.
用法
Dialog.accept();
Dialog.accept(promptText);
参数
返回
defaultValue
Added before v1.9如果对话是提示,则返回默认提示值。否则,返回空字符串。
🌐 If dialog is prompt, returns default prompt value. Otherwise, returns empty string.
用法
Dialog.defaultValue();
返回
dismiss
Added before v1.9当对话框关闭时返回。
🌐 Returns when the dialog has been dismissed.
用法
Dialog.dismiss();
返回
message
Added before v1.9对话框中显示一条消息。
🌐 A message displayed in the dialog.
用法
Dialog.message();
返回
page
Added in: v1.34启动此对话框的页面(如果可用)。
🌐 The page that initiated this dialog, if available.
用法
Dialog.page();
返回
type
Added before v1.9返回对话的类型,可以是 alert、beforeunload、confirm 或 prompt。
🌐 Returns dialog's type, can be one of alert, beforeunload, confirm or prompt.
用法
Dialog.type();
返回