跳至主要内容

对话框

Dialog 对象由页面通过 Page.Dialog 事件分发。

使用Dialog类的一个示例:

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()来处理对话框 - 否则页面会冻结等待对话框响应,点击等操作将无法完成。


方法

AcceptAsync

Added before v1.9 dialog.AcceptAsync

当对话框被接受时返回。

用法

await Dialog.AcceptAsync(promptText);

参数

  • promptText string? (可选)#

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

返回


默认值

Added before v1.9 dialog.DefaultValue

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

用法

Dialog.DefaultValue

返回


DismissAsync

Added before v1.9 dialog.DismissAsync

当对话框被关闭时返回。

用法

await Dialog.DismissAsync();

返回


消息

Added before v1.9 dialog.Message

对话框中显示的消息。

用法

Dialog.Message

返回


页面

Added in: v1.34 dialog.Page

触发此对话框的页面(如果存在)。

用法

Dialog.Page

返回


类型

Added before v1.9 dialog.Type

返回对话框的类型,可以是以下之一:alertbeforeunloadconfirmprompt

用法

Dialog.Type

返回