IPEasyUtility.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Dynamic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using OpenQA.Selenium;
  11. using OpenQA.Selenium.Chrome;
  12. using OpenQA.Selenium.Support.Extensions;
  13. using OpenQA.Selenium.Support.UI;
  14. namespace wispro.sp.utility
  15. {
  16. public class IPEasyUtility
  17. {
  18. public static void DownloadReport(string strId,string filename)
  19. {
  20. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  21. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  22. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  23. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  24. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  25. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  26. options.AddUserProfilePreference("intl.accept_languages", "nl");
  27. options.AddUserProfilePreference("disable-popup-blocking", "true");
  28. if (isheadless)
  29. {
  30. options.AddArgument("headless");
  31. }
  32. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  33. {
  34. try
  35. {
  36. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
  37. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
  38. //进入登录界面
  39. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  40. //输入用户名和密码
  41. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  42. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  43. //点击登录按钮
  44. driver.FindElement(By.Id("btnLogin")).Click();
  45. //关闭提示遮罩层
  46. driver.FindElement(By.Id("jpwClose")).Click();
  47. //点击顶部菜单栏中的报表管理菜单
  48. driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9")).Click();
  49. //点击左侧 自定义报表 菜单
  50. driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b")).Click();
  51. //切换到自定义报表Frame
  52. driver.SwitchTo().Frame(1);
  53. //调用报表导出JS
  54. ((IJavaScriptExecutor)driver).ExecuteScript($"Report.Export('{strId}');");
  55. //切换到弹出的导出报表窗口,点击导出按钮
  56. driver.SwitchTo().DefaultContent();
  57. var ihg_export = driver.FindElement(By.Name("ihg_export"));
  58. driver.SwitchTo().Frame(ihg_export);
  59. driver.FindElement(By.Id("btnSubmit")).Click();
  60. string strFilePath = System.IO.Path.Combine(strFileSavePath, filename);
  61. while (true)
  62. {
  63. System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
  64. if (!file.Exists || file.Length == 0)
  65. {
  66. System.Threading.Thread.Sleep(5000);
  67. }
  68. else
  69. {
  70. break;
  71. }
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. throw new Exception(ex.Message);
  77. }
  78. finally
  79. {
  80. driver.Quit();
  81. driver.Dispose();
  82. killChromProcess();
  83. }
  84. }
  85. }
  86. private static void Log(string strMessage)
  87. {
  88. StreamWriter sw = File.AppendText("c:\\temp\\log.txt");
  89. sw.WriteLine($"{strMessage}");
  90. sw.Flush();
  91. sw.Close();
  92. sw.Dispose();
  93. }
  94. /// <summary>
  95. /// 根据报表名称导出报表
  96. /// </summary>
  97. /// <param name="ReportName">报告名称</param>
  98. /// <param name="isModifyDate">是否手动修改日期范围</param>
  99. public static DataTable DownloadReport(string ReportName,bool isModifyDate)
  100. {
  101. Log($"{DateTime.Now}\t开始下载:{ReportName}");
  102. DataTable retDatatable;
  103. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  104. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  105. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  106. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  107. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  108. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  109. options.AddUserProfilePreference("intl.accept_languages", "nl");
  110. options.AddUserProfilePreference("disable-popup-blocking", "true");
  111. if (isheadless)
  112. {
  113. options.AddArgument("headless");
  114. }
  115. Log($"{DateTime.Now}\t开始启动Chrome");
  116. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  117. {
  118. try
  119. {
  120. Log($"{DateTime.Now}\tIWebDriver配置");
  121. driver.Manage().Window.Maximize();
  122. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
  123. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(500);
  124. //进入登录界面
  125. Log($"{DateTime.Now}\t开始进入登录界面");
  126. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  127. //输入用户名和密码
  128. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  129. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  130. //点击登录按钮
  131. Log($"{DateTime.Now}\t开始点击登录按钮");
  132. driver.FindElement(By.Id("btnLogin")).Click();
  133. //关闭提示遮罩层
  134. Log($"{DateTime.Now}\t关闭提示遮罩层");
  135. driver.FindElement(By.Id("jpwClose")).Click();
  136. //点击顶部菜单栏中的报表管理菜单
  137. Log($"{DateTime.Now}\t点击顶部菜单栏中的报表管理菜单");
  138. var reportMenu = driver.FindElement(By.Name("970d33d5-c728-41b8-a060-4330610706b9"));
  139. driver.ExecuteJavaScript("arguments[0].click();", reportMenu);
  140. //点击左侧 自定义报表 菜单
  141. Log($"{DateTime.Now}\t点击左侧 自定义报表 菜单");
  142. var customerReportMenu = driver.FindElement(By.Name("642fa96f-1e1f-46fd-aaa4-cb461ee8df5b"));
  143. driver.ExecuteJavaScript("arguments[0].click();", customerReportMenu);
  144. //切换到自定义报表Frame
  145. Log($"{DateTime.Now}\t切换到自定义报表Frame");
  146. driver.SwitchTo().Frame(1);
  147. Log($"{DateTime.Now}\t开始搜索报告");
  148. var inputSearch = driver.FindElement(By.Id("customizedList_TxtSheetSearchKey"));
  149. Log($"{DateTime.Now}\t在搜索框中输入报告名称");
  150. inputSearch.SendKeys(ReportName);
  151. Log($"{DateTime.Now}\t点击搜索按钮");
  152. var btnSearch = driver.FindElement(By.ClassName("btn-search"));
  153. btnSearch.Click();
  154. Log($"{DateTime.Now}\t选中报告");
  155. var reportRecord = driver.FindElement(By.XPath($"//td[contains(text(),'{ReportName}')]"));
  156. reportRecord.Click();
  157. System.Threading.Thread.Sleep(1000);
  158. if (isModifyDate)
  159. {
  160. Log($"{DateTime.Now}\t输入报告时间");
  161. var btnEdit = driver.FindElement(By.ClassName("edit"));
  162. btnEdit.Click();
  163. System.Threading.Thread.Sleep(500);
  164. var doItemFinished = driver.FindElement(By.XPath("//span[contains(@objcvalue,'_date') and contains(@id,'ST;')]"));
  165. doItemFinished.Click();
  166. System.Threading.Thread.Sleep(500);
  167. var startDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_s_')]"));
  168. startDate.Click();
  169. System.Threading.Thread.Sleep(500);
  170. startDate.SendKeys(new DateTime(DateTime.Now.AddMonths(-1).Year, DateTime.Now.AddMonths(-1).Month, 1).ToString("yyyy-MM-dd"));
  171. var endDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_e_')]"));//.Id("dt_e_4F8FE88D-9040-45F1-9723-45699BCD4CAF"));
  172. endDate.Click();
  173. System.Threading.Thread.Sleep(500);
  174. endDate.SendKeys(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).ToString("yyyy-MM-dd"));
  175. var addDate = driver.FindElement(By.XPath("//a[contains(@onclick,'ReportSub.ConditionDTSelect')]"));
  176. addDate.Click();
  177. System.Threading.Thread.Sleep(500);
  178. var btnSave = driver.FindElement(By.Id("save"));
  179. btnSave.Click();
  180. System.Threading.Thread.Sleep(3000);
  181. }
  182. Log($"{DateTime.Now}\t点击导出Excel链接");
  183. var linkExport = driver.FindElement(By.XPath("//a[contains(@onclick,'-') and @class='tbexcel']"));
  184. linkExport.Click();
  185. //切换到弹出的导出报表窗口,点击导出按钮
  186. Log($"{DateTime.Now}\t切换到弹出的导出报表窗口,点击导出按钮");
  187. driver.SwitchTo().DefaultContent();
  188. var ihg_export = driver.FindElement(By.Name("ihg_export"));
  189. driver.SwitchTo().Frame(ihg_export);
  190. driver.FindElement(By.Id("btnSubmit")).Click();
  191. //切换到弹出的下载报表界面,点击下载按钮
  192. Log($"{DateTime.Now}\t切换到弹出的下载报表界面,点击下载按钮");
  193. System.Threading.Thread.Sleep(3000);
  194. driver.SwitchTo().DefaultContent();
  195. var frameDownload = driver.FindElement(By.Name("DownloadList"));
  196. driver.SwitchTo().Frame(frameDownload);
  197. var firstTr = driver.FindElement(By.TagName("tr"));
  198. var tdStatus = firstTr.FindElement(By.XPath("//td")).FindElement(By.XPath("following-sibling::td[4]"));
  199. string strStatus = tdStatus.Text;
  200. while (strStatus.Trim() != "导出成功!")
  201. {
  202. System.Threading.Thread.Sleep(5000);
  203. firstTr = driver.FindElement(By.TagName("tr"));
  204. tdStatus = firstTr.FindElement(By.XPath("//td")).FindElement(By.XPath("following-sibling::td[4]"));
  205. strStatus = tdStatus.Text;
  206. }
  207. Log($"{DateTime.Now}\t点击下载按钮下载文档");
  208. firstTr = driver.FindElement(By.TagName("tr"));
  209. firstTr.FindElement(By.XPath("//td/a[@title='下载']")).Click();
  210. //System.Threading.Thread.Sleep(5000);
  211. //btnDownload.Click();
  212. string strFilePath = System.IO.Path.Combine(strFileSavePath, $"{ReportName.Trim()}.xlsx");
  213. int iwaiting = 0;
  214. while (true)
  215. {
  216. System.IO.FileInfo file = new System.IO.FileInfo(strFilePath.Replace("~", "_"));
  217. if (!file.Exists || file.Length == 0)
  218. {
  219. System.Threading.Thread.Sleep(5000);
  220. iwaiting += 5000;
  221. if (iwaiting > 360000)
  222. {
  223. break;
  224. }
  225. }
  226. else
  227. {
  228. break;
  229. }
  230. }
  231. //删除下载记录
  232. Log($"{DateTime.Now}\t删除下载记录");
  233. firstTr = driver.FindElement(By.TagName("tr"));
  234. firstTr.FindElement(By.XPath("//td/a[@title='删除']")).Click();
  235. //System.IO.FileInfo file = new System.IO.FileInfo(strFilePath.Replace("~", "_"));
  236. retDatatable = NPOIExcel.ExcelToDataTable(strFilePath.Replace("~", "_"),true);
  237. System.IO.File.Delete(strFilePath.Replace("~", "_"));
  238. }
  239. catch (Exception ex)
  240. {
  241. Log(ex.ToString());
  242. throw new Exception(ex.Message, ex);
  243. }
  244. finally
  245. {
  246. Log("关闭Chrome");
  247. driver.Quit();
  248. driver.Dispose();
  249. killChromProcess();
  250. }
  251. }
  252. Log($"{DateTime.Now}\t返回数据");
  253. return retDatatable;
  254. }
  255. /// <summary>
  256. /// 获取案件基本信息
  257. /// </summary>
  258. /// <param name="caseNo">我方文号</param>
  259. /// <returns></returns>
  260. public static dynamic GetCaseInfo(string caseNo)
  261. {
  262. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  263. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  264. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  265. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  266. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  267. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  268. options.AddUserProfilePreference("intl.accept_languages", "nl");
  269. options.AddUserProfilePreference("disable-popup-blocking", "true");
  270. options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
  271. if (isheadless)
  272. {
  273. options.AddArgument("headless");
  274. }
  275. dynamic retObject = new ExpandoObject();
  276. retObject.CaseNo = caseNo.Trim();
  277. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath"), options))
  278. {
  279. try
  280. {
  281. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
  282. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(100);
  283. //进入登录界面
  284. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  285. //输入用户名和密码
  286. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  287. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  288. //点击登录按钮
  289. driver.FindElement(By.Id("btnLogin")).Click();
  290. //关闭提示遮罩层
  291. driver.FindElement(By.Id("jpwClose")).Click();
  292. try
  293. {
  294. var okBtn = driver.FindElement(By.ClassName("ui_state_highlight"));
  295. if (okBtn != null && okBtn.TagName == "input")
  296. {
  297. okBtn.Click();
  298. }
  299. }
  300. catch { }
  301. //点击顶部菜单栏中的案件管理菜单
  302. driver.FindElement(By.Name("71A7CC35-F597-40E1-9FEF-BE622A3A3B63")).Click();
  303. //点击左侧 查询 菜单
  304. driver.FindElement(By.Name("c3266ab3-521a-4815-8aaf-7dd0bc5a76af")).Click();
  305. //切换到自定义报表Frame
  306. driver.SwitchTo().Frame(1);
  307. var inputSearch = driver.FindElement(By.Id("case_volume"));
  308. inputSearch.SendKeys(caseNo.Trim());
  309. var btnSearch = driver.FindElement(By.Id("btn_Search"));
  310. btnSearch.Click();
  311. var caseLink = driver.FindElement(By.XPath($"//a[contains(text(),'{caseNo}')]"));
  312. caseLink.Click();
  313. driver.SwitchTo().ParentFrame().SwitchTo().Frame(2);
  314. WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
  315. //等待页面上ID属性值为submitButton的元素加载完成
  316. IWebElement myElement = wait.Until((d) =>
  317. {
  318. try
  319. {
  320. driver.SwitchTo().ParentFrame().SwitchTo().Frame(2);
  321. return d.FindElement(By.Id("libase"));
  322. }
  323. catch {
  324. return null;
  325. }
  326. });
  327. myElement.Click();
  328. //driver.FindElement(By.Id("libase")).Click();
  329. retObject.CaseName = driver.FindElement(By.Id("p_case_info__case_name")).GetAttribute("value"); //案件名称
  330. retObject.CustomerName = driver.FindElement(By.Id("p_case_info__customer_id")).GetAttribute("value"); //客户名称
  331. retObject.BusinessType = driver.FindElement(By.Id("p_case_info__business_type_id")).GetAttribute("value"); //业务类型
  332. retObject.CaseState = GetSelectText(driver.FindElement(By.Id("p_case_info__case_status_id")));
  333. retObject.ApplicationType = GetSelectText(driver.FindElement(By.Id("p_case_info__apply_type_id")));
  334. retObject.CaseType = driver.FindElement(By.Id("p_case_info__case_type_id")).GetAttribute("value"); //案件类型
  335. retObject.EntrustingDate = driver.FindElement(By.Id("p_case_info__charge_date")).GetAttribute("value"); //委案日期
  336. retObject.CaseMemo = driver.FindElement(By.Id("p_case_info__remark")).GetAttribute("value"); //案件备注
  337. driver.FindElement(By.XPath($"//span[contains(text(),'扩展信息')]")).Click();
  338. //select[@id='p_case_info__case_coefficient_id']
  339. retObject.CaseCoefficient = GetSelectText(driver.FindElement(By.Id("p_case_info__case_coefficient_id")));
  340. }
  341. catch (Exception ex)
  342. {
  343. throw new Exception(ex.Message, ex);
  344. }
  345. finally
  346. {
  347. driver.Quit();
  348. killChromProcess();
  349. }
  350. }
  351. return retObject;
  352. }
  353. public static dynamic GetPerformanceRecord(string caseNo, List<string> doItemNames, string caseStage = null)
  354. {
  355. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  356. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  357. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  358. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  359. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  360. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  361. options.AddUserProfilePreference("intl.accept_languages", "nl");
  362. options.AddUserProfilePreference("disable-popup-blocking", "true");
  363. options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
  364. if (isheadless)
  365. {
  366. options.AddArgument("headless");
  367. }
  368. dynamic retObject = new ExpandoObject();
  369. retObject.CaseNo = caseNo.Trim();
  370. //retObject.DoItem = doItemName;
  371. using (var Service = ChromeDriverService.CreateDefaultService(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath")))
  372. {
  373. Service.Start();
  374. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(Service, options))
  375. {
  376. try
  377. {
  378. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
  379. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(100);
  380. //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(100);
  381. WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
  382. //进入登录界面
  383. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  384. //输入用户名和密码
  385. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  386. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  387. //点击登录按钮
  388. driver.FindElement(By.Id("btnLogin")).Click();
  389. //关闭提示遮罩层
  390. driver.FindElement(By.Id("jpwClose")).Click();
  391. //try
  392. //{
  393. // var okBtn = driver.FindElement(By.ClassName("ui_state_highlight"));
  394. // if (okBtn != null && okBtn.TagName == "input")
  395. // {
  396. // okBtn.Click();
  397. // }
  398. //}
  399. //catch { }
  400. //点击顶部菜单栏中的案件管理菜单
  401. driver.FindElement(By.Name("71A7CC35-F597-40E1-9FEF-BE622A3A3B63")).Click();
  402. System.Threading.Thread.Sleep(500);
  403. //点击左侧 查询 菜单
  404. driver.FindElement(By.Name("c3266ab3-521a-4815-8aaf-7dd0bc5a76af")).Click();
  405. //System.Threading.Thread.Sleep(500);
  406. var inputSearch = wait.Until((d) =>
  407. {
  408. try
  409. {
  410. //切换到自定义报表Frame
  411. d.SwitchTo().Frame(1);
  412. return d.FindElement(By.Id("case_volume"));
  413. }
  414. catch
  415. {
  416. return null;
  417. }
  418. });
  419. //切换到自定义报表Frame
  420. //driver.SwitchTo().Frame(1);
  421. //var inputSearch = driver.FindElement(By.Id("case_volume"));
  422. inputSearch.SendKeys(caseNo.Trim());
  423. var btnSearch = driver.FindElement(By.Id("btn_Search"));
  424. btnSearch.Click();
  425. var caseLink = wait.Until((d) =>
  426. {
  427. try
  428. {
  429. return d.FindElement(By.XPath($"//a[normalize-space()='{caseNo.Trim()}']"));
  430. }
  431. catch
  432. {
  433. return null;
  434. }
  435. });
  436. //System.Threading.Thread.Sleep(500);
  437. //var caseLink = driver.FindElement(By.XPath($"//a[normalize-space()='{caseNo.Trim()}']"));
  438. caseLink.Click();
  439. System.Threading.Thread.Sleep(500);
  440. driver.SwitchTo().ParentFrame().SwitchTo().Frame(2);
  441. IWebElement temDoItemLink = null;
  442. foreach (string doItemName in doItemNames)
  443. {
  444. var DoItemLinks = driver.FindElements(By.XPath($"//td[@colname='ctrl_proc'][normalize-space()='{doItemName}']"));
  445. if (DoItemLinks.Count > 0)
  446. {
  447. if (!string.IsNullOrEmpty(caseStage))
  448. {
  449. foreach (var DoItemLink in DoItemLinks)
  450. {
  451. var temCaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text;
  452. if (temCaseStage == caseStage)
  453. {
  454. retObject.CaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text; //案件阶段
  455. retObject.DoItemState = DoItemLink.FindElement(By.XPath("following-sibling::td[2]")).Text; //处理事项处理状态
  456. retObject.InternalDate = DoItemLink.FindElement(By.XPath("following-sibling::td[4]")).Text; //内部期限
  457. retObject.CustomerLimitDate = DoItemLink.FindElement(By.XPath("following-sibling::td[5]")).Text; //客户期限
  458. retObject.FinishedDate = DoItemLink.FindElement(By.XPath("following-sibling::td[7]")).Text; //处理事项完成日
  459. retObject.DoPersons = DoItemLink.FindElement(By.XPath("following-sibling::td[8]")).Text; //处理人
  460. retObject.DoItemMemo = DoItemLink.FindElement(By.XPath("following-sibling::td[9]")).Text; //处理事项备注
  461. retObject.Reviewer = DoItemLink.FindElement(By.XPath("following-sibling::td[10]")).Text; //核稿人
  462. System.Threading.Thread.Sleep(4000);
  463. temDoItemLink = DoItemLink;
  464. //driver.ExecuteJavaScript("$(arguments[0]).click()", DoItemLink);
  465. break;
  466. }
  467. }
  468. }
  469. else
  470. {
  471. var DoItemLink = DoItemLinks[DoItemLinks.Count - 1];
  472. retObject.CaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text; //案件阶段
  473. retObject.DoItemState = DoItemLink.FindElement(By.XPath("following-sibling::td[2]")).Text; //处理事项处理状态
  474. retObject.InternalDate = DoItemLink.FindElement(By.XPath("following-sibling::td[4]")).Text; //内部期限
  475. retObject.CustomerLimitDate = DoItemLink.FindElement(By.XPath("following-sibling::td[5]")).Text; //客户期限
  476. retObject.FinishedDate = DoItemLink.FindElement(By.XPath("following-sibling::td[7]")).Text; //处理事项完成日
  477. retObject.DoPersons = DoItemLink.FindElement(By.XPath("following-sibling::td[8]")).Text; //处理人
  478. retObject.DoItemMemo = DoItemLink.FindElement(By.XPath("following-sibling::td[9]")).Text; //处理事项备注
  479. retObject.Reviewer = DoItemLink.FindElement(By.XPath("following-sibling::td[10]")).Text; //核稿人
  480. temDoItemLink = DoItemLink;
  481. //new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(DoItemLink)).Click();
  482. }
  483. retObject.DoItem = doItemName;
  484. break;
  485. }
  486. }
  487. if(temDoItemLink != null)
  488. {
  489. System.Threading.Thread.Sleep(4000);
  490. temDoItemLink.Click();
  491. }
  492. else
  493. {
  494. throw new ApplicationException("处理事项不存在!");
  495. }
  496. var p_proc_finish_doc_date = wait.Until((d) =>
  497. {
  498. try
  499. {
  500. return d.FindElement(By.Id("p_proc_info__finish_doc_date"));
  501. }
  502. catch
  503. {
  504. return null;
  505. }
  506. });
  507. //System.Threading.Thread.Sleep(500);
  508. retObject.FinalizationDate = p_proc_finish_doc_date.GetAttribute("value"); //定稿日
  509. retObject.ReturnDate = driver.FindElement(By.Id("p_proc_info__back_date")).GetAttribute("value"); //返稿日
  510. retObject.Reviewer = driver.FindElement(By.Id("p_proc_info__revise_user")).GetAttribute("value"); //核稿人
  511. retObject.DoItemCoefficient = driver.FindElement(By.Id("p_proc_info__proc_coefficient")).GetAttribute("value"); //处理事项系数
  512. retObject.WordCount = driver.FindElement(By.Id("p_proc_info__translate_count")).GetAttribute("value"); //翻译字数
  513. driver.FindElement(By.Id("libase")).Click();
  514. var p_case_info__case_name = wait.Until((d) =>
  515. {
  516. try
  517. {
  518. return d.FindElement(By.Id("p_case_info__case_name"));
  519. }
  520. catch
  521. {
  522. return null;
  523. }
  524. });
  525. //System.Threading.Thread.Sleep(500);
  526. retObject.CaseName = p_case_info__case_name.GetAttribute("value"); //案件名称
  527. retObject.CustomerName = driver.FindElement(By.Id("p_case_info__customer_id")).GetAttribute("value"); //客户名称
  528. retObject.BusinessType = driver.FindElement(By.Id("p_case_info__business_type_id")).GetAttribute("value"); //业务类型
  529. retObject.CaseState = GetSelectText(driver.FindElement(By.Id("p_case_info__case_status_id")));
  530. retObject.ApplicationType = GetSelectText(driver.FindElement(By.Id("p_case_info__apply_type_id")));
  531. retObject.CaseType = driver.FindElement(By.Id("p_case_info__case_type_id")).GetAttribute("value"); //案件类型
  532. retObject.EntrustingDate = driver.FindElement(By.Id("p_case_info__charge_date")).GetAttribute("value"); //委案日期
  533. retObject.CaseMemo = driver.FindElement(By.Id("p_case_info__remark")).GetAttribute("value"); //案件备注
  534. driver.FindElement(By.XPath($"//span[contains(text(),'扩展信息')]")).Click();
  535. //select[@id='p_case_info__case_coefficient_id']
  536. retObject.CaseCoefficient = GetSelectText(driver.FindElement(By.Id("p_case_info__case_coefficient_id")));
  537. }
  538. catch (Exception ex)
  539. {
  540. throw new Exception(ex.Message, ex);
  541. }
  542. finally
  543. {
  544. driver.Quit();
  545. killChromProcess();
  546. }
  547. }
  548. Service.Dispose();
  549. }
  550. return retObject;
  551. }
  552. /// <summary>
  553. /// 获取案件处理事项记录
  554. /// </summary>
  555. /// <param name="caseNo">我方文号</param>
  556. /// <param name="doItemName">处理事项</param>
  557. /// <returns></returns>
  558. public static dynamic GetPerformanceRecord(string caseNo,string doItemName,string caseStage=null)
  559. {
  560. string strFileSavePath = ConfigHelper.GetSectionValue("IPEasySetting:DownloadFileSavePath");
  561. bool isheadless = (ConfigHelper.GetSectionValue("IPEasySetting:isHeadless") == "true");
  562. string Account = ConfigHelper.GetSectionValue("IPEasySetting:Account");
  563. string Password = ConfigHelper.GetSectionValue("IPEasySetting:Password");
  564. OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
  565. options.AddUserProfilePreference("download.default_directory", strFileSavePath);
  566. options.AddUserProfilePreference("intl.accept_languages", "nl");
  567. options.AddUserProfilePreference("disable-popup-blocking", "true");
  568. options.AddUserProfilePreference("profile", new { default_content_setting_values = new { images = 2 } });
  569. if (isheadless)
  570. {
  571. options.AddArgument("headless");
  572. }
  573. dynamic retObject = new ExpandoObject();
  574. retObject.CaseNo = caseNo.Trim();
  575. retObject.DoItem = doItemName;
  576. using (var Service = ChromeDriverService.CreateDefaultService(ConfigHelper.GetSectionValue("IPEasySetting:ChormeDriverPath")))
  577. {
  578. Service.Start();
  579. using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(Service, options))
  580. {
  581. try
  582. {
  583. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
  584. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(100);
  585. //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(100);
  586. WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
  587. //进入登录界面
  588. driver.Navigate().GoToUrl(ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb"));
  589. //输入用户名和密码
  590. driver.FindElement(By.Id("txtUser")).SendKeys(Account);
  591. driver.FindElement(By.Id("txtPwd")).SendKeys(Password);
  592. //点击登录按钮
  593. driver.FindElement(By.Id("btnLogin")).Click();
  594. //关闭提示遮罩层
  595. driver.FindElement(By.Id("jpwClose")).Click();
  596. //try
  597. //{
  598. // var okBtn = driver.FindElement(By.ClassName("ui_state_highlight"));
  599. // if (okBtn != null && okBtn.TagName == "input")
  600. // {
  601. // okBtn.Click();
  602. // }
  603. //}
  604. //catch { }
  605. //点击顶部菜单栏中的案件管理菜单
  606. driver.FindElement(By.Name("71A7CC35-F597-40E1-9FEF-BE622A3A3B63")).Click();
  607. System.Threading.Thread.Sleep(500);
  608. //点击左侧 查询 菜单
  609. driver.FindElement(By.Name("c3266ab3-521a-4815-8aaf-7dd0bc5a76af")).Click();
  610. //System.Threading.Thread.Sleep(500);
  611. var inputSearch = wait.Until((d) =>
  612. {
  613. try
  614. {
  615. //切换到自定义报表Frame
  616. d.SwitchTo().Frame(1);
  617. return d.FindElement(By.Id("case_volume"));
  618. }
  619. catch
  620. {
  621. return null;
  622. }
  623. });
  624. //切换到自定义报表Frame
  625. //driver.SwitchTo().Frame(1);
  626. //var inputSearch = driver.FindElement(By.Id("case_volume"));
  627. inputSearch.SendKeys(caseNo.Trim());
  628. var btnSearch = driver.FindElement(By.Id("btn_Search"));
  629. btnSearch.Click();
  630. var caseLink = wait.Until((d) =>
  631. {
  632. try
  633. {
  634. return d.FindElement(By.XPath($"//a[normalize-space()='{caseNo.Trim()}']"));
  635. }
  636. catch
  637. {
  638. return null;
  639. }
  640. });
  641. //System.Threading.Thread.Sleep(500);
  642. //var caseLink = driver.FindElement(By.XPath($"//a[normalize-space()='{caseNo.Trim()}']"));
  643. caseLink.Click();
  644. System.Threading.Thread.Sleep(500);
  645. driver.SwitchTo().ParentFrame().SwitchTo().Frame(2);
  646. var DoItemLinks = driver.FindElements(By.XPath($"//td[@colname='ctrl_proc'][normalize-space()='{doItemName}']"));
  647. if (DoItemLinks.Count > 0)
  648. {
  649. if (!string.IsNullOrEmpty(caseStage))
  650. {
  651. foreach (var DoItemLink in DoItemLinks)
  652. {
  653. var temCaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text;
  654. if (temCaseStage == caseStage)
  655. {
  656. retObject.CaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text; //案件阶段
  657. retObject.DoItemState = DoItemLink.FindElement(By.XPath("following-sibling::td[2]")).Text; //处理事项处理状态
  658. retObject.InternalDate = DoItemLink.FindElement(By.XPath("following-sibling::td[4]")).Text; //内部期限
  659. retObject.CustomerLimitDate = DoItemLink.FindElement(By.XPath("following-sibling::td[5]")).Text; //客户期限
  660. retObject.FinishedDate = DoItemLink.FindElement(By.XPath("following-sibling::td[7]")).Text; //处理事项完成日
  661. retObject.DoPersons = DoItemLink.FindElement(By.XPath("following-sibling::td[8]")).Text; //处理人
  662. retObject.DoItemMemo = DoItemLink.FindElement(By.XPath("following-sibling::td[9]")).Text; //处理事项备注
  663. retObject.Reviewer = DoItemLink.FindElement(By.XPath("following-sibling::td[10]")).Text; //核稿人
  664. System.Threading.Thread.Sleep(4000);
  665. DoItemLink.Click();
  666. //driver.ExecuteJavaScript("$(arguments[0]).click()", DoItemLink);
  667. break;
  668. }
  669. }
  670. }
  671. else
  672. {
  673. var DoItemLink = DoItemLinks[DoItemLinks.Count - 1];
  674. retObject.CaseStage = DoItemLink.FindElement(By.XPath("following-sibling::td[1]")).Text; //案件阶段
  675. retObject.DoItemState = DoItemLink.FindElement(By.XPath("following-sibling::td[2]")).Text; //处理事项处理状态
  676. retObject.InternalDate = DoItemLink.FindElement(By.XPath("following-sibling::td[4]")).Text; //内部期限
  677. retObject.CustomerLimitDate = DoItemLink.FindElement(By.XPath("following-sibling::td[5]")).Text; //客户期限
  678. retObject.FinishedDate = DoItemLink.FindElement(By.XPath("following-sibling::td[7]")).Text; //处理事项完成日
  679. retObject.DoPersons = DoItemLink.FindElement(By.XPath("following-sibling::td[8]")).Text; //处理人
  680. retObject.DoItemMemo = DoItemLink.FindElement(By.XPath("following-sibling::td[9]")).Text; //处理事项备注
  681. retObject.Reviewer = DoItemLink.FindElement(By.XPath("following-sibling::td[10]")).Text; //核稿人
  682. System.Threading.Thread.Sleep(4000);
  683. DoItemLink.Click();
  684. //new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(DoItemLink)).Click();
  685. }
  686. }
  687. var p_proc_finish_doc_date = wait.Until((d) =>
  688. {
  689. try
  690. {
  691. return d.FindElement(By.Id("p_proc_info__finish_doc_date"));
  692. }
  693. catch
  694. {
  695. return null;
  696. }
  697. });
  698. //System.Threading.Thread.Sleep(500);
  699. retObject.FinalizationDate = p_proc_finish_doc_date.GetAttribute("value"); //定稿日
  700. retObject.ReturnDate = driver.FindElement(By.Id("p_proc_info__back_date")).GetAttribute("value"); //返稿日
  701. retObject.Reviewer = driver.FindElement(By.Id("p_proc_info__revise_user")).GetAttribute("value"); //核稿人
  702. retObject.DoItemCoefficient = driver.FindElement(By.Id("p_proc_info__proc_coefficient")).GetAttribute("value"); //处理事项系数
  703. retObject.WordCount = driver.FindElement(By.Id("p_proc_info__translate_count")).GetAttribute("value"); //翻译字数
  704. driver.FindElement(By.Id("libase")).Click();
  705. var p_case_info__case_name = wait.Until((d) =>
  706. {
  707. try
  708. {
  709. return d.FindElement(By.Id("p_case_info__case_name"));
  710. }
  711. catch
  712. {
  713. return null;
  714. }
  715. });
  716. //System.Threading.Thread.Sleep(500);
  717. retObject.CaseName = p_case_info__case_name.GetAttribute("value"); //案件名称
  718. retObject.CustomerName = driver.FindElement(By.Id("p_case_info__customer_id")).GetAttribute("value"); //客户名称
  719. retObject.BusinessType = driver.FindElement(By.Id("p_case_info__business_type_id")).GetAttribute("value"); //业务类型
  720. retObject.CaseState = GetSelectText(driver.FindElement(By.Id("p_case_info__case_status_id")));
  721. retObject.ApplicationType = GetSelectText(driver.FindElement(By.Id("p_case_info__apply_type_id")));
  722. retObject.CaseType = driver.FindElement(By.Id("p_case_info__case_type_id")).GetAttribute("value"); //案件类型
  723. retObject.EntrustingDate = driver.FindElement(By.Id("p_case_info__charge_date")).GetAttribute("value"); //委案日期
  724. retObject.CaseMemo = driver.FindElement(By.Id("p_case_info__remark")).GetAttribute("value"); //案件备注
  725. driver.FindElement(By.XPath($"//span[contains(text(),'扩展信息')]")).Click();
  726. //select[@id='p_case_info__case_coefficient_id']
  727. retObject.CaseCoefficient = GetSelectText(driver.FindElement(By.Id("p_case_info__case_coefficient_id")));
  728. }
  729. catch (Exception ex)
  730. {
  731. throw new Exception(ex.Message, ex);
  732. }
  733. finally
  734. {
  735. driver.Quit();
  736. killChromProcess();
  737. }
  738. }
  739. Service.Dispose();
  740. }
  741. return retObject;
  742. }
  743. private static void killChromProcess()
  744. {
  745. try
  746. {
  747. Process[] workers = Process.GetProcessesByName("Chrome");
  748. foreach (Process worker in workers)
  749. {
  750. worker.Kill();
  751. worker.WaitForExit();
  752. worker.Dispose();
  753. }
  754. workers = Process.GetProcessesByName("Chromedriver");
  755. foreach (Process worker in workers)
  756. {
  757. worker.Kill();
  758. worker.WaitForExit();
  759. worker.Dispose();
  760. }
  761. }
  762. catch { }
  763. }
  764. private static string GetSelectText(IWebElement element)
  765. {
  766. var strValue = element.GetAttribute("value");
  767. if (string.IsNullOrEmpty(strValue))
  768. {
  769. return null;
  770. }
  771. var ops = element.FindElements(By.TagName("option"));
  772. foreach (var op in ops)
  773. {
  774. if (op.GetAttribute("value") == strValue)
  775. {
  776. return op.Text;
  777. }
  778. }
  779. return null;
  780. }
  781. }
  782. }