使用Selenium Webdriver ChromeDriver下载和打开文件时发生异常

时间:2018-08-04 04:44:03

标签: c# excel selenium-webdriver

我正在尝试使用Selenium Webdriver chromedriver从C#控制台应用程序自动下载excel文件。代码如下:

ChromeOptions chromeops = new ChromeOptions();
            chromeops.AddUserProfilePreference("download.default_directory", Properties.Settings.Default.DownloadFiles);
            chromeops.AddArgument("no-sandbox");
            driver = new ChromeDriver(@"C:\Users\...\bin\Debug", chromeops, TimeSpan.FromMinutes(2));
            LogManager.log_entry("Attempting to login");

            driver.Url = "https://url/";
            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2000);
            IWebElement userNameBox, passwordBox;
            userNameBox = driver.FindElement(By.Id("userName"));
            System.Threading.Thread.Sleep(1000);
            userNameBox.SendKeys(Properties.Settings.Default.USERNAME);
            passwordBox = driver.FindElement(By.Id("password"));
            System.Threading.Thread.Sleep(1000);
            passwordBox.SendKeys(Properties.Settings.Default.PASSWORD + OpenQA.Selenium.Keys.Enter);

            LogManager.log_entry("Downloading Data File");

            driver.FindElement(By.Id("1_A")).Click();
            web_element = driver.FindElement(By.Id("1_99_A"));
            Actions click_builder;
            click_builder = new Actions(driver);
            click_builder.ContextClick(web_element).Build().Perform();
            driver.FindElement(By.Id("ShowDiv_A")).Click(); //download data file
            web_element = null;
            click_builder = null;

现在下载的文件是一个html文件,然后使用以下代码将其转换为excel文件:

file_name = new DirectoryInfo(download_dir).GetFiles().OrderByDescending(f => f.LastWriteTime).First().Name;

                string file_path = download_dir + @"\" + file_name;
                DateTime file_Creation_time = File.GetCreationTime(file_path);
                LogManager.log_entry("File Path : " + file_path);
                LogManager.log_entry("Processing File");
                excel = new Microsoft.Office.Interop.Excel.Application();
                sheet = excel.Workbooks.Open(file_path);
                String strNewFileName = Path.GetDirectoryName(file_path) + @"\" + Path.GetFileNameWithoutExtension(file_path) + @"_SAVED.xlsx";
                LogManager.log_entry("Converting File to XLSX format");
                sheet.SaveAs(strNewFileName, 51);
                sheet.Close(false, Type.Missing, Type.Missing);
                excel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                excel = null;

                file_name = new DirectoryInfo(Properties.Settings.Default._Location).GetFiles().OrderByDescending(f => f.LastWriteTime).First().Name;
                file_path = Properties.Settings.Default._Location + @"\" + file_name;
                return uploadData("Status", file_path, file_Creation_time.ToString("dd-MMM-yyyy hh:mm:ss tt"));

同时1.将文件转换为excel格式,并2.在移动文件时,我发现该文件正被其他进程使用。请检查引发的异常:

  

31_Jul_18_08_02_01_AM:尝试登录
  31_Jul_18_08_02_10_AM:下载数据文件
  31_Jul_18_08_02_30_AM:1个文件未正确下载
  31_Jul_18_08_02_30_AM:System.IO.IOException:该进程无法访问文件“ data.xls.crdownload”,因为它正在被另一个进程使用。      在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)      在System.IO.FileInfo.Delete()      在project.Upload_Manager.manageDataMovement()中的c:\ Users ... \ Upload_Manager.cs:line 106

似乎我在完全下载文件之前尝试访问该文件。我该如何支票?欢迎任何解决方法。

0 个答案:

没有答案