如何验证正在下载的文件。单击“下载”按钮后,我陷入了如何检索下载的文件的困境。
'' driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
driver.Navigate().GoToUrl("http://192.162.0.1/testing-admin/Login/Login.aspx");
driver.FindElement(By.Id("ctl00_MainContent_ucLogin_txtUserID")).SendKeys("Jojo");
driver.FindElement(By.Id("ctl00_MainContent_ucLogin_txtPassword")).SendKeys("Man15742368");
driver.FindElement(By.Id("ctl00_MainContent_ucLogin_cmdLogin")).Click();
driver.Navigate().GoToUrl("http://192.162.0.1/testing-admin/User_Document/User_Document_Download.aspx");
driver.FindElement(By.XPath("//a[@id='ctl00_MainContent_GV_ctl02_lnkDownloadFile']")).Click(); //download button
var Path = "drive path";
ChromeOptions co = new ChromeOptions();
co.AddAdditionalCapability("download.default_directory", Path);
driver = new ChromeDriver(co);
然后您可以使用 System.IO.DirectoryInfo 将所有下载的文件详细信息检索到 Path 文件夹中。
我用过这个,也有效。但唯一的缺点是您必须知道下载的文件的名称。
String myDownloadFolder = @"c:\temp\";
var options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", myDownloadFolder);
driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("http://google/download/some"); // download some stuffs
driver.FindElement(By.LinkText("Download")).Click();
System.Threading.Thread.Sleep(10000);
Assert.IsTrue(File.Exists(@"c:\temp\Test.docx"));
以下是设置 ChromeDriver 并验证文件是否已成功下载的命令(使用最新 v121 进行测试)-
Directory.CreateDirectory(fullPath);
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", fullPath);
chromeOptions.AddAdditionalOption("download.download.prompt_for_download", false);
driver = new ChromeDriver(chromeOptions);
// navigate to page and interact with it - download will happen automatically
File.Exists($"{fullPath}\{fileName}").Should().BeTrue();
注意:目前似乎无法在隐身模式下工作