You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 line
1.5KB

  1. using System;
  2. using System.Reflection;
  3. using System.IO;
  4. using OpenQA.Selenium;
  5. using OpenQA.Selenium.Chrome;
  6. using OpenQA.Selenium.Support.UI;
  7. using SeleniumExtras.WaitHelpers;
  8. namespace CSVDownloader.Web {
  9. class DriverFactory {
  10. public static ChromeDriver GetDriver() {
  11. CreateDir();
  12. var options = new ChromeOptions();
  13. // options.AddArgument("--headless");
  14. options.BinaryLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\chrome-win64\\chrome.exe";
  15. String download_dir = GetTmpDownloadDir();
  16. options.AddUserProfilePreference("download.default_directory", download_dir);
  17. ChromeDriver driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
  18. WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
  19. return driver;
  20. }
  21. public static String GetTmpDownloadDir() {
  22. return Path.GetFullPath("tmp_download");
  23. }
  24. public static String GetDownloadDir() {
  25. return Path.GetFullPath("download");
  26. }
  27. private static void CreateDir() {
  28. String tmpdownload_dir = GetTmpDownloadDir();
  29. String download_dir = GetDownloadDir();
  30. if (!Directory.Exists(tmpdownload_dir)) {
  31. Directory.CreateDirectory(tmpdownload_dir);
  32. }
  33. if (!Directory.Exists(download_dir)) {
  34. Directory.CreateDirectory(download_dir);
  35. }
  36. }
  37. }
  38. }