1,前言
搞这个Demo整整搞几天通宵,爆肝了。后做的效果出来,还是不错的。给小伙伴看看效果图。
2, 遇到的问题
- 日期之间相差多少个月数。开始时间框点击对应月份要点击多少次,结束时间框点击对应月份要点击多少次
- Xpath获取问题。找到对应html元素包含那个元素,比如 querySelectorAll('td[class$="view"]')。
3,C# 代码
日期之间可能出现的情况:
// 第1种 :2023-11-1 到 2023-11-1
// 第2种 :2023-11-1 到 2023-12-25
// 第3种 :2023-11-1 到 2024-1-15
// 第4种 :2023-10-29 到 2024-1-15
// 1, 获取到时间
string star_time = StartTime.Text;
string end_time = EndTime.Text;// 2, 点击这个自定义时间 弹出时间框
var btss = csbrowser.EvaluateScript(chromiumWebBrowser1.GetMainFrame(), Properties.Resources.ClickSelectTime);
csbrowser.WaitBusy(chromiumWebBrowser1, 30, 1000);// 3, 获取弹出时间框 当前时间
string start_Year_Month_code = "document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-header > div > button.cook-picker-month-btn\")";
var start_Year_Month = csbrowser.EvaluateScript(chromiumWebBrowser1.GetMainFrame(), Properties.Resources.GetCustomTime);List<SelectTime> selectTimes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SelectTime>>(start_Year_Month.ToString());
SelectTime selectTime_start = new SelectTime();
SelectTime selectTime_end = new SelectTime();
if (selectTimes==null)
{return;
}
foreach (SelectTime item in selectTimes)
{if (item.StartOrEnd_Time_Type == StartOrEnd_Time.Start_Time){List<string> dd = new List<string>();if (star_time.Contains("-")){dd = star_time.Split('-')?.ToList();}else if (star_time.Contains(".")){dd = star_time.Split('.')?.ToList();}if (dd.Count == 3){selectTime_start.Year_Time = dd[0];selectTime_start.Month_Time = dd[1];selectTime_start.Day_Time = dd[2];if (int.TryParse(selectTime_start.Year_Time, out int input_Year) && int.TryParse(selectTime_start.Month_Time, out int input_Month) && int.TryParse(selectTime_start.Day_Time, out int input_Day) && int.TryParse(item.Year_Time, out int 弹视框_start_year) && int.TryParse(item.Month_Time, out int 弹视框_start_Month) && int.TryParse(item.Day_Time, out int 弹视框_start_Day)){Task task = null;//对比选择上个时间得DateTime date1 = new DateTime(input_Year, input_Month, input_Day); // 第一个日期对象DateTime date2 = new DateTime(弹视框_start_year, 弹视框_start_Month, 弹视框_start_Day); // 第二个日期对象int ccc = MonthCount(date1, date2);var months = ((date2.Year - date1.Year) * 12) + date2.Month - date1.Month;task = Task.Run(() =>{});int yearDiff = Math.Abs((date1 - date2).Days); // 计算年份之间的差值(取绝对值)int monthDiff = (date1 - date2).Days;decimal decimal_oo = Math.Floor((decimal)yearDiff / 30);if (yearDiff < 30){decimal_oo = Math.Ceiling((decimal)yearDiff / 30);if (input_Year == 弹视框_start_year && input_Month == 弹视框_start_Month){csbrowser.WaitBusy(chromiumWebBrowser1, 30, 1000);task = Task.Run(() =>{});}else{decimal_oo = months;//csbrowser.WaitBusy(chromiumWebBrowser1, 30, 1000);task = Task.Run(() =>{for (int i = 0; i < decimal_oo; i++){var start_Year_Month_num = csbrowser.EvaluateScript(chromiumWebBrowser1.GetMainFrame(), Properties.Resources.ClickStart_Moth_Time);}});}}else{if (yearDiff < 30){decimal_oo = Math.Ceiling((decimal)Math.Abs(yearDiff) / 30);}else{decimal_oo = Math.Round((decimal)Math.Abs(yearDiff) / 30);//decimal_oo = Math.Floor((decimal)Math.Abs(yearDiff) / 30);}decimal_oo = months;csbrowser.WaitBusy(chromiumWebBrowser1, 30, 1000);task = Task.Run(() =>{//chromiumWebBrowser1.GetMainFrame().ExecuteJavaScriptAsync( Properties.Resources.JavaScript1, "13215313335");//csbrowser.WaitBusy(chromiumWebBrowser1.GetFrame(, 30, 1000);for (int i = 0; i < decimal_oo; i++){var start_Year_Month_num = csbrowser.EvaluateScript(chromiumWebBrowser1.GetMainFrame(), Properties.Resources.ClickStart_Moth_Time);}});}string code_click_num = "var num_click = " + input_Day.ToString() + ";var result = document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-body\").querySelectorAll('td[class$=\"view\"]');for (let index = 0; index < result.length; index++){const element = result[index].outerText;if (num_click == element){result[index].click();}}";if (task.Wait(5000)){chromiumWebBrowser1.GetMainFrame().ExecuteJavaScriptAsync(code_click_num);}}}}if (item.StartOrEnd_Time_Type == StartOrEnd_Time.End_Time){List<string> dd = new List<string>();if (end_time.Contains("-")){dd = end_time.Split('-')?.ToList();}else if(end_time.Contains(".")){dd = end_time.Split('.')?.ToList();}//SelectTime selectTime = new SelectTime();if (dd.Count == 3){// selectTime_end.Year_Time = dd[0];selectTime_end.Month_Time = dd[1];selectTime_end.Day_Time = dd[2];// 选择定开启时间后,再var end_Year_Month_num = csbrowser.EvaluateScript(chromiumWebBrowser1.GetMainFrame(), Properties.Resources.NewGetEndTime);if (end_Year_Month_num!=null && end_Year_Month_num.StartsWith("{")){// 获取弹视框里的时间SelectTime 弹视框_End_Times = Newtonsoft.Json.JsonConvert.DeserializeObject<SelectTime>(end_Year_Month_num.ToString());if (弹视框_End_Times != null ){弹视框_End_Times.Day_Time = "15";if (int.TryParse(弹视框_End_Times.Year_Time, out int 弹视框_End_Times_Year) && int.TryParse(弹视框_End_Times.Month_Time, out int 弹视框_End_Times_Month) && int.TryParse(弹视框_End_Times.Day_Time, out int 弹视框_End_Times_Day) && int.TryParse(selectTime_end.Year_Time, out int input_end_Year2) && int.TryParse(selectTime_end.Month_Time, out int input_end_Month2) && int.TryParse(selectTime_end.Day_Time, out int input_end_Day2) && int.TryParse(selectTime_start.Year_Time, out int input_start_Year) && int.TryParse(selectTime_start.Month_Time, out int input_start_Month) && int.TryParse(selectTime_start.Day_Time, out int input_start_Day)){Task task = null;输入的开始时间和 输入的end的时间 进行 对比DateTime date1 = new DateTime(input_start_Year, input_start_Month, input_start_Day); // 第一个日期对象DateTime date2 = new DateTime(input_end_Year2, input_end_Month2, input_end_Day2); // 第二个日期对象int yearDiff = Math.Abs((date1 - date2).Days); // 计算年份之间的差值(取绝对值)int monthDiff = (date1 - date2).Days;decimal decimal_oo = Math.Floor((decimal)yearDiff / 30);if (yearDiff < 30){task = Task.Run(() =>{});decimal_oo = Math.Ceiling((decimal)yearDiff / 30);if (decimal_oo == 0){//MessageBox.Show("yearDiff < 30 " + decimal_oo.ToString());//点击start框的时间框 string code_click_num = "var num_click = " + input_end_Day2.ToString() + ";var result = document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-body\").querySelectorAll('td[class$=\"cook-picker-cell-range-start-single\"]');for (let index = 0; index < result.length; index++){const element = result[index].outerText;if (num_click == element){result[index].click();}}";if (task.Wait(5000)){chromiumWebBrowser1.GetMainFrame().ExecuteJavaScriptAsync(code_click_num);}}else{// MessageBox.Show("yearDiff < 30");//就是同一个 开始时间框//点击开始时间框string code_click_num = "var num_click = " + input_end_Day2.ToString() + ";var result = document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-body\").querySelectorAll('td[class$=\"view\"]');for (let index = 0; index < result.length; index++){const element = result[index].outerText;if (num_click == element){result[index].click();}}";if (input_start_Year == input_end_Year2 && input_start_Month == input_end_Month2){code_click_num = "var num_click = " + input_end_Day2.ToString() + ";var result = document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-body\").querySelectorAll('td[class$=\"view\"]');for (let index = 0; index < result.length; index++){const element = result[index].outerText;if (num_click == element){result[index].click();}}";}else{code_click_num = "var num_click = " + input_end_Day2.ToString() + ";var result = document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(2) > div > div.cook-picker-body\").querySelectorAll('td[class$=\"view\"]');for (let index = 0; index < result.length; index++){const element = result[index].outerText;if (num_click == element){result[index].click();}}";}if (task.Wait(5000)){chromiumWebBrowser1.GetMainFrame().ExecuteJavaScriptAsync(code_click_num);}}}else// 超过31天的{csbrowser.WaitBusy(chromiumWebBrowser1, 30, 1000);//输入的开始时间和 弹视框里的时间 进行 对比,是否点击下一个月。DateTime date_弹视框 = new DateTime(弹视框_End_Times_Year, 弹视框_End_Times_Month, 弹视框_End_Times_Day); // 第二个日期对象var months = ((date2.Year - date_弹视框.Year) * 12) + date2.Month - date_弹视框.Month;if (date_弹视框.Year == date2.Year && date_弹视框.Month == date2.Month){//MessageBox.Show("date_弹视框.Year == date2.Year && date_弹视框.Month == date2.Month");task = Task.Run(() =>{});// 点击弹视框里的时间string code_click_num = "var num_click = " + input_end_Day2.ToString() + ";var result = document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(2) > div > div.cook-picker-body\").querySelectorAll('td[class$=\"view\"]');for (let index = 0; index < result.length; index++){const element = result[index].outerText;if (num_click == element){result[index].click();}}";if (task.Wait(5000)){chromiumWebBrowser1.GetMainFrame().ExecuteJavaScriptAsync(code_click_num);}}else{decimal_oo = months;task = Task.Run(() =>{for (int i = 0; i < decimal_oo; i++){var start_Year_Month_num = csbrowser.EvaluateScript(chromiumWebBrowser1.GetMainFrame(), Properties.Resources.ClickEnd_Moth_Time);}});// 点击弹视框里的时间string code_click_num = "var num_click = " + input_end_Day2.ToString() + ";var result = document.querySelector(\"body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(2) > div > div.cook-picker-body\").querySelectorAll('td[class$=\"view\"]');for (let index = 0; index < result.length; index++){const element = result[index].outerText;if (num_click == element){result[index].click();}}";if (task.Wait(5000)){chromiumWebBrowser1.GetMainFrame().ExecuteJavaScriptAsync(code_click_num);}}}}}}}}}
4,Js 代码
1, 获取点击按钮弹出时间框里开始和结束时间,文件GetCustomTime.js
var start_Year_code = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-header > div > button.cook-picker-year-btn");var start_Year_Month_code = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-header > div > button.cook-picker-month-btn");var start_Year_Month_Day_code = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-body > table > tbody > tr:nth-child(3) > td.cook-picker-cell.cook-picker-cell-disabled.cook-picker-cell-in-view.cook-picker-cell-today > div");var result = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-body").querySelectorAll('td[class$="cook-picker-cell-today"]');if (result != undefined && result.length > 0) {//alert(result[0].outerText);start_Year_Month_Day_code = result[0];
}var start_Year_Time = start_Year_code != undefined ? start_Year_code.outerText.replace("年", '') : "";
var start_Month_Time = start_Year_Month_code != undefined ? start_Year_Month_code.outerText.replace("月", '') : "";
var start_Day_Time = start_Year_Month_Day_code != undefined ? start_Year_Month_Day_code.outerText : "";var end_Year_code = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(2) > div > div.cook-picker-header > div > button.cook-picker-year-btn");
var end_Year_Month_code = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(2) > div > div.cook-picker-header > div > button.cook-picker-month-btn");var end_Year_Time = end_Year_code != undefined ? end_Year_code.outerText.replace("年", '') : "";
var end_Month_Time = end_Year_Month_code != undefined ? end_Year_Month_code.outerText.replace("月", '') : "";var json = [{"Year_Time": start_Year_Time,"Month_Time": start_Month_Time,"Day_Time": start_Day_Time,"StartOrEnd_Time_Type": "Start_Time"},{"Year_Time": end_Year_Time,"Month_Time": end_Month_Time,"Day_Time": start_Day_Time,"StartOrEnd_Time_Type": "End_Time"},];[JSON.stringify(json)].toString();
2,点击开始时间框的按钮跳转月数,文件ClickStart_Moth_Time.js
var click_name = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(1) > div > div.cook-picker-header > button:nth-child(2)");
if (click_name != undefined) {click_name.click();
}[click_name != undefined ? click_name.innerHTML:"", is_ok_click].toString();
3,点击结束时间框的按钮跳转月数,文件ClickEnd_Moth_Time.js
var click_name = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(2) > div > div.cook-picker-header > button.cook-picker-header-next-btn");
if (click_name != undefined) {click_name.click();
}[click_name != undefined ? click_name.innerHTML:"", is_ok_click].toString();
4,点击开始时间框的按钮跳转月数后,再获取结束时间框里的时间,NewGetEndTime.js
var click_name = document.querySelector("body > div:nth-child(7) > div > div > div > div.cook-picker-panel-container > div > div:nth-child(2) > div > div.cook-picker-header > button.cook-picker-header-next-btn");
if (click_name != undefined) {click_name.click();
}[click_name != undefined ? click_name.innerHTML:"", is_ok_click].toString();
该文为原创文章,转载请注明原文出处
Author:靓仔建 w: gaolian1025
(待业中,希望帮助到各位,大家的支持是创作最大动力!)