Playwright.Net下载文件,使用WaitForDownloadAsync

 

Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
var userDataDir = $"{Directory.GetCurrentDirectory()}\\UserData";
var context = await Playwright.Chromium.LaunchPersistentContextAsync(userDataDir,
new BrowserTypeLaunchPersistentContextOptions
{
Headless = false,
AcceptDownloads=true,
ExecutablePath = SystemConsts.ChromeExecutablePath,
ViewportSize = new ViewportSize()
{
Width = 1920,
Height = 1080
}
//Args = new List<string> { "--start-maximized" }
}
);

 

 

public static async Task DownAsync()
{
var waitForDownloadTask = Page.WaitForDownloadAsync();

await Page.ClickAsync("text=下载");
var download = await waitForDownloadTask;
await download.SaveAsAsync(download.SuggestedFilename);
}

posted on 2024-08-12 11:43  daconglee  阅读(15)  评论(0编辑  收藏  举报