Skip to main content

Dialog

[对话] 对象通过页面的 Page.Dialog 事件分发。

使用 Dialog 类的示例:

🌐 An example of using Dialog class:

using Microsoft.Playwright;
using System.Threading.Tasks;

class DialogExample
{
public static async Task Run()
{
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();

page.Dialog += async (_, dialog) =>
{
System.Console.WriteLine(dialog.Message);
await dialog.DismissAsync();
};

await page.EvaluateAsync("alert('1');");
}
}
note

对话框会自动关闭,除非存在 Page.Dialog 监听器。当监听器存在时,必须要么 Dialog.AcceptAsync() 要么 Dialog.DismissAsync() 对话框——否则页面将会 冻结 等待对话框,点击等操作将永远无法完成。


方法

🌐 Methods

AcceptAsync

Added before v1.9 dialog.AcceptAsync

当对话框被接受时返回。

🌐 Returns when the dialog has been accepted.

用法

await Dialog.AcceptAsync(promptText);

参数

  • promptText string? (optional)#

    要在提示中输入的文本。如果对话的 type 不是提示,则不会产生任何效果。可选。

返回


DefaultValue

Added before v1.9 dialog.DefaultValue

如果对话是提示,则返回默认提示值。否则,返回空字符串。

🌐 If dialog is prompt, returns default prompt value. Otherwise, returns empty string.

用法

Dialog.DefaultValue

返回


DismissAsync

Added before v1.9 dialog.DismissAsync

当对话框关闭时返回。

🌐 Returns when the dialog has been dismissed.

用法

await Dialog.DismissAsync();

返回


Message

Added before v1.9 dialog.Message

对话框中显示一条消息。

🌐 A message displayed in the dialog.

用法

Dialog.Message

返回


Page

Added in: v1.34 dialog.Page

启动此对话框的页面(如果可用)。

🌐 The page that initiated this dialog, if available.

用法

Dialog.Page

返回


Type

Added before v1.9 dialog.Type

返回对话的类型,可以是 alertbeforeunloadconfirmprompt

🌐 Returns dialog's type, can be one of alert, beforeunload, confirm or prompt.

用法

Dialog.Type

返回