在 selenium 和 c# 中等待完成的下载文件

Raf*_*lQA 3 c# selenium wait

我有一些问题。单击 Web 应用程序上的图标后,我下载了一个文件。我的下一步是在下载记录文件之前执行的。我要等到文件下载完成?

有谁知道如何等待?

Dmi*_*try 5

我使用以下脚本(应传入文件名)。

第一部分等待文件出现在磁盘上(适用于 chrome)

第二部分等到它停止变化(并开始有一些内容)

var downloadsPath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\Downloads\" + fileName;
for (var i = 0; i < 30; i++)
{
    if (File.Exists(downloadsPath)) { break; }
    Thread.Sleep(1000);
}
var length = new FileInfo(downloadsPath).Length;
for (var i = 0; i < 30; i++)
{
    Thread.Sleep(1000);
    var newLength = new FileInfo(downloadsPath).Length;
    if (newLength == length && length != 0) { break; }
    length = newLength;
}
Run Code Online (Sandbox Code Playgroud)