Skip to main content

Dialog

Dialog 对象通过 Page.Dialog 事件按页分派。

¥Dialog objects are dispatched by page via the Page.Dialog event.

使用 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');");
}
}
注意

除非有 Page.Dialog 监听器,否则对话框会自动关闭。当监听器存在时,它必须是 Dialog.AcceptAsync()Dialog.DismissAsync() 对话框 - 否则页面将 freeze 等待对话框,并且单击等操作将永远不会完成。

¥Dialogs are dismissed automatically, unless there is a Page.Dialog listener. When listener is present, it must either Dialog.AcceptAsync() or Dialog.DismissAsync() the dialog - otherwise the page will freeze waiting for the dialog, and actions like click will never finish.


方法

¥Methods

AcceptAsync

Added before v1.9 dialog.AcceptAsync

当对话框被接受时返回。

¥Returns when the dialog has been accepted.

用法

¥Usage

await Dialog.AcceptAsync(promptText);

参数

¥Arguments

在提示中输入的文本。如果对话框的 type 没有提示,不会造成任何影响。可选的。

¥A text to enter in prompt. Does not cause any effects if the dialog's type is not prompt. Optional.

返回

¥Returns


DefaultValue

Added before v1.9 dialog.DefaultValue

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

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

用法

¥Usage

Dialog.DefaultValue

返回

¥Returns


DismissAsync

Added before v1.9 dialog.DismissAsync

当对话框关闭时返回。

¥Returns when the dialog has been dismissed.

用法

¥Usage

await Dialog.DismissAsync();

返回

¥Returns


消息

¥Message

Added before v1.9 dialog.Message

对话框中显示一条消息。

¥A message displayed in the dialog.

用法

¥Usage

Dialog.Message

返回

¥Returns


Page

Added in: v1.34 dialog.Page

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

¥The page that initiated this dialog, if available.

用法

¥Usage

Dialog.Page

返回

¥Returns


类型

¥Type

Added before v1.9 dialog.Type

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

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

用法

¥Usage

Dialog.Type

返回

¥Returns