简体   繁体   English

无法使用 C# 语言在编剧自动化中下载文件

[英]Unable to download file in playwright Automation using C# language

I am not able to download file from browser using playwright automation.我无法使用剧作家自动化从浏览器下载文件。
I am using C# language for automation.我正在使用 C# 语言进行自动化。 I have created browser context with acceptDownload and also provided directory path for file download.我已经使用 acceptDownload 创建了浏览器上下文,还提供了文件下载的目录路径。 Basically code is stuck at RunAndWaitForDownloadAsync method and noting is happening after that.基本上,代码停留在 RunAndWaitForDownloadAsync 方法中,之后会发生注意事项。 Please find below which I have used to download file.请在下面找到我用来下载文件的内容。

//While creating browser context
    
var playwright = await Playwright.CreateAsync();  
var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions()   
{ 
  Headless = false,   
  DownloadsPath = Directory.GetCurrentDirectory() 
});
    
var context = await browser.NewContextAsync(new BrowserNewContextOptions() { AcceptDownloads = true });
    
//For file download used below code.
    
var download = await _page.RunAndWaitForDownloadAsync(async () =>
{
   await _page.ClickAsync("#btnId");
}, new PageRunAndWaitForDownloadOptions() { Timeout = 10000 });
    
var tmpDir = Directory.GetCurrentDirectory();
string userPath = Path.Combine(tmpDir, "download.docx");
await download.SaveAsAsync(userPath);    
string path = await download.PathAsync();
   Assert.True(new FileInfo(path).Exists);
    
//One more option tried as below.
    
var downloadTask = _page.WaitForDownloadAsync();
await WhenAll(downloadTask, _page.ClickAsync("#btnSubmission")).ConfigureAwait(false);
var download = downloadTask.Result;
var tmpDir = Directory.GetCurrentDirectory();
string userPath = Path.Combine(tmpDir, "download.docx");
await download.SaveAsAsync(userPath);
string path = await download.PathAsync();
Assert.True(new FileInfo(path).Exists);
    
//Supporting method
public static async Task<T> WhenAll<T>(Task<T> task1, Task task2)
{
  await Task.WhenAll(task1, task2).ConfigureAwait(false);
  return task1.Result;
}

This is simplified code simulating downloading files using Playwright in C#.这是模拟使用 Playwright 在 C# 下载文件的简化代码。
Microsoft.Playwright NuGet extension is used in this example.本示例使用 Microsoft.Playwright NuGet 扩展名。

private async void DownloadTest()
{
    const string url = "https://file-examples.com/index.php/sample-documents-download/sample-doc-download/";
    var PlayWright = await Playwright.CreateAsync();
    BrowserTypeLaunchOptions browserLaunchOptions = new BrowserTypeLaunchOptions() { Headless = false, Devtools = true };
    var Browser = await PlayWright.Chromium.LaunchAsync(browserLaunchOptions);
    BrowserNewContextOptions browserContextOptions = new BrowserNewContextOptions() { AcceptDownloads = true };
    var Context = await Browser.NewContextAsync(browserContextOptions);
    var Page = await Context.NewPageAsync();
    Page.Download += Page_Download;
    await Page.GotoAsync(url, new PageGotoOptions() { WaitUntil = WaitUntilState.NetworkIdle });
    await Page.EvaluateAsync("document.getElementsByTagName('table')[0].getElementsByTagName('a')[0].click();");
}

// Download files
private async void Page_Download(object sender, IDownload e)
{
    string fileName = e.SuggestedFilename;
    await e.SaveAsAsync(e.SuggestedFilename);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM