IPEasyUtility.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Linq;
  3. using OpenQA.Selenium;
  4. namespace wispro.sp.utility
  5. {
  6. public class IPEasyUtility
  7. {
  8. public static void DownloadReport(string strId,string filename)
  9. {
  10. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  11. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  12. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  13. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  14. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  15. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  16. options.AddUserProfilePreference("intl.accept_languages", "nl");
  17. options.AddUserProfilePreference("disable-popup-blocking", "true");
  18. if (isheadless)
  19. {
  20. options.AddArgument("headless");
  21. }
  22. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  23. {
  24. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
  25. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
  26. //进入登录界面
  27. driver.Navigate().GoToUrl("http://47.106.221.167/Login.aspx");
  28. //输入用户名和密码
  29. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  30. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  31. //点击登录按钮
  32. driver.FindElement(By.Id("btnLogin")).Click();
  33. //关闭提示遮罩层
  34. driver.FindElement(By.Id("jpwClose")).Click();
  35. //点击顶部菜单栏中的报表管理菜单
  36. driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
  37. //点击左侧 自定义报表 菜单
  38. driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
  39. //切换到自定义报表Frame
  40. driver.SwitchTo().Frame(1);
  41. //调用报表导出JS
  42. ((IJavaScriptExecutor)driver).ExecuteScript($"Report.Export('{strId}');");
  43. //切换到弹出的导出报表窗口,点击导出按钮
  44. driver.SwitchTo().DefaultContent();
  45. var ihg_export = driver.FindElement(By.Name("ihg_export"));
  46. driver.SwitchTo().Frame(ihg_export);
  47. driver.FindElement(By.Id("btnSubmit")).Click();
  48. string strFilePath = System.IO.Path.Combine(strFileSavePath, filename);
  49. while (true)
  50. {
  51. System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
  52. if (!file.Exists || file.Length == 0) {
  53. System.Threading.Thread.Sleep(5000);
  54. }
  55. else
  56. {
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }