IPEasyUtility.cs 47 KB

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