瀏覽代碼

修改下载文件时设定结束时间出错和保持文件时Chrome提示“已阻止不安全的下载”导致不能下载文件的问题

luocaiyang 11 月之前
父節點
當前提交
d6225350e6

+ 1 - 1
wispro.sp.api/appsettings.json

@@ -30,7 +30,7 @@
     "isHeadless": "false",
     "Account": "caiyangl",
     "Password": "j)wx*lier*@3",
-    "ChormeDriverPath": "e:\\source\\repos\\StaffPerformance\\packages\\ChormeDriver\\128.0.6613.86",
+    "ChormeDriverPath": "d:\\source\\repos\\StaffPerformance\\packages\\ChormeDriver\\130.0.6723.69",
     "ScheduleSetting": "00 55 15 16 * ? *",
     "IPEasyWeb": "http://47.106.94.35/Login.aspx"
   },

+ 29 - 2
wispro.sp.utility/IPEasyUtility.cs

@@ -128,10 +128,12 @@ namespace wispro.sp.utility
 
             OpenQA.Selenium.Chrome.ChromeOptions options = new OpenQA.Selenium.Chrome.ChromeOptions();
 
-
+            
             options.AddUserProfilePreference("download.default_directory", strFileSavePath);
             options.AddUserProfilePreference("intl.accept_languages", "nl");
             options.AddUserProfilePreference("disable-popup-blocking", "true");
+            options.AddUserProfilePreference("safebrowsing.enabled", true);
+            options.AddArgument($"--unsafely-treat-insecure-origin-as-secure={ConfigHelper.GetSectionValue("IPEasySetting:IPEasyWeb").Replace("/Login.aspx","")}");//替换自己的域名,多域名可用逗号分隔。
             if (isheadless)
             {
                 options.AddArgument("headless");
@@ -141,6 +143,11 @@ namespace wispro.sp.utility
             {
                 try
                 {
+                    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
+                    driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(100);
+                    //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(100);
+                    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
+
                     Log($"{DateTime.Now}\tIWebDriver配置");
                     driver.Manage().Window.Maximize();
                     driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(50);
@@ -218,7 +225,22 @@ namespace wispro.sp.utility
                         startDate.SendKeys(new DateTime(DateTime.Now.AddMonths(-1).Year, DateTime.Now.AddMonths(-1).Month, 1).ToString("yyyy-MM-dd"));
 
                         Log($"{DateTime.Now}\t输入处理事项完成日期:结束日期");
-                        var endDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_e_')]"));//.Id("dt_e_4F8FE88D-9040-45F1-9723-45699BCD4CAF"));
+
+                        var endDate = wait.Until((d) =>
+                        {
+                            try
+                            {
+                                return d.FindElement(By.XPath("//input[contains(@id,'dt_e_')]"));//.Id("dt_e_4F8FE88D-9040-45F1-9723-45699BCD4CAF"));
+                            }
+                            catch
+                            {
+                                return null;
+                            }
+
+                        });
+                        //var endDate = driver.FindElement(By.XPath("//input[contains(@id,'dt_e_')]"));//.Id("dt_e_4F8FE88D-9040-45F1-9723-45699BCD4CAF"));
+                        System.Threading.Thread.Sleep(20000);
+                        //driver.ExecuteJavaScript("arguments[0].click();", endDate);
                         endDate.Click();
                         System.Threading.Thread.Sleep(500);
                         endDate.SendKeys(new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1).ToString("yyyy-MM-dd"));
@@ -258,6 +280,11 @@ namespace wispro.sp.utility
                     string strStatus = tdStatus.Text;
                     while (strStatus.Trim() != "导出成功!")
                     {
+                        if(strStatus.Trim() == "导出失败,请稍后重试!")
+                        {
+                            return null;
+                        }
+
                         System.Threading.Thread.Sleep(5000);
                         firstTr = driver.FindElement(By.TagName("tr"));
                         tdStatus = firstTr.FindElement(By.XPath("//td")).FindElement(By.XPath("following-sibling::td[4]"));

+ 4 - 2
wispro.sp.winClient/Form1.Designer.cs

@@ -46,7 +46,7 @@ namespace wispro.sp.winClient
             button1.Name = "button1";
             button1.Size = new System.Drawing.Size(304, 79);
             button1.TabIndex = 0;
-            button1.Text = "合并反馈文档";
+            button1.Text = "测试IPEasy获取数据";
             button1.UseVisualStyleBackColor = true;
             button1.Click += button1_Click;
             // 
@@ -57,7 +57,7 @@ namespace wispro.sp.winClient
             button2.Name = "button2";
             button2.Size = new System.Drawing.Size(304, 79);
             button2.TabIndex = 0;
-            button2.Text = "输入绩效基本点数";
+            button2.Text = "测试从IPEasy下载文件数据";
             button2.UseVisualStyleBackColor = true;
             button2.Click += button2_Click;
             // 
@@ -70,6 +70,7 @@ namespace wispro.sp.winClient
             button3.TabIndex = 0;
             button3.Text = "计算绩效";
             button3.UseVisualStyleBackColor = true;
+            button3.Visible = false;
             button3.Click += button3_Click;
             // 
             // button5
@@ -125,6 +126,7 @@ namespace wispro.sp.winClient
             button9.TabIndex = 1;
             button9.Text = "获取一次OA授权数据";
             button9.UseVisualStyleBackColor = true;
+            button9.Visible = false;
             button9.Click += button9_Click;
             // 
             // Form1

+ 11 - 0
wispro.sp.winClient/Form1.cs

@@ -51,6 +51,17 @@ namespace wispro.sp.winClient
 
         private void button2_Click(object sender, EventArgs e)
         {
+            DataTable dt = IPEasyUtility.DownloadReport("每月绩效统计--上个月递交完成案件", true);
+            if(dt !=null)
+            {
+                MessageBox.Show($"获取到数据:{dt.Rows.Count}行!");
+            }
+            else
+            {
+                MessageBox.Show($"获取到数据失败!");
+            }
+            
+            return;
             OpenFileDialog ofd = new OpenFileDialog()
             {
                 Multiselect = false,

+ 1 - 1
wispro.sp.winClient/appsettings.json

@@ -4,7 +4,7 @@
     "isHeadless": "false",
     "Account": "caiyangl",
     "Password": "j)wx*lier*@3",
-    "ChormeDriverPath": "D:\\source\\repos\\StaffPerformance\\packages\\ChormeDriver\\123.0.6312.86",
+    "ChormeDriverPath": "D:\\source\\repos\\StaffPerformance\\packages\\ChormeDriver\\130.0.6723.69",
     "ScheduleSetting": "00 55 10 3 * ? *",
     "IPEasyWeb": "http://47.106.94.35/Login.aspx"
   },

+ 6 - 0
wispro.sp.winClient/wispro.sp.winClient.csproj

@@ -15,4 +15,10 @@
     <ProjectReference Include="..\wospro.sp.entity\wispro.sp.entity.csproj" />
   </ItemGroup>
 
+  <ItemGroup>
+    <None Update="appsettings.json">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+  </ItemGroup>
+
 </Project>