繁体   English   中英

如何验证是否在Selenium Webdriver C#中下载文件

[英]How do I verify if a file is being downloaded in Selenium Webdriver C#

如何验证正在下载的文件。 我被困在如何点击“下载”按钮时检索下载的文件。

''  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"));

暂无
暂无

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

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