文件目录结构:
主体内容:
openChromeDriver.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import static java.lang.Thread.*;
public class openChromeDriver {
// ①先赋值下需要提交申请的日期
public static String[] datelist={"2021-12-29 09:00","2021-12-29 18:00","2021-12-30 09:00","2021-12-30 18:00","2021-12-31 09:00","2021-12-31 18:00"};
// ②登录用户名和密码设置
private static String username=userInfo.userName;
private static String userpwd=userInfo.pwd;
public static StringBuffer stringBuffer = new StringBuffer();
public static String days;
public static void main(String[] args) {
dayLists.init();
datelist=dayLists.datelist;
buka();
System.out.println("\n datelist 数组是 \n "+Arrays.toString(datelist) + "\ndatelist.length = " +datelist.length);
}
/**
* 补卡整体流程
*/
public static void buka(){
System.setProperty("webdriver.chrome.driver","/Users/jojo/Documents/idea/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("URL地址");//写自己需要打开的URL全路径
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement name=driver.findElement(By.id("login_username"));//*[@id="login_username"
WebElement pwd=driver.findElement(By.id("login_password"));//*[@id="login_validatePwdStrength"
name.sendKeys(username);
pwd.sendKeys(userpwd);
WebElement loginButton=driver.findElement(By.id("login_button"));
loginButton.click();
/**
* 循环开始
*/
for(int i=0;i<datelist.length;i++){
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// ---------
//循环开始
String timeString=datelist[i].split("\"")[1];
System.out.println("即将提交: "+timeString);
WebElement kaoqin=driver.findElement(By.xpath("//*[@id=\"topMenuNavContainer\"]/ul[1]/li[4]/div[1]"));
kaoqin.click();
WebElement bukashenqing=driver.findElement(By.id("000_/collabora_-7531217030663812387"));
bukashenqing.click();
try {
sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//获取当前窗口handleId
String WindowsOne =driver.getWindowHandle();
//切换到新window
for(String tempHandleId: driver.getWindowHandles())
{
//如果不是当前窗口的thisHandleId,那么我们就用它取切换到新窗口
if(!tempHandleId.equals(WindowsOne))
{
driver.switchTo().window(tempHandleId);
}
}
driver.switchTo().frame("zwIframe");//进入子页面,填写表单内容
WebElement time=driver.findElement(By.xpath("//*[@id=\"field0005\"]"));
time.click();
time.sendKeys(timeString); //输入时间
WebElement typeIcon=driver.findElement(By.xpath("//*[@id=\"field0016\"]/div[2]/i"));//下拉框按钮
typeIcon.click();
WebElement typeText=driver.findElement(By.xpath("//*[@id=\"field0016_scnt\"]/div[2]/div"));
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebDriverWait wait = new WebDriverWait(driver, 3000);
typeText.click();
WebElement reason=driver.findElement(By.xpath("//*[@id=\"field0006_id\"]/section/div[2]/div/textarea[1]"));
reason.sendKeys("app异常");
driver.switchTo().parentFrame();//退出 zwIframe 页面,发送按钮在父页面
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement sendBtn=driver.findElement(By.id("btnCustomSend"));
sendBtn.click();//发送表单
// //获取所有窗口句柄
// Set<String> handles=driver.getWindowHandles();
// //获取当前窗口handleId
// thisHandleId =driver.getWindowHandle();
//回到上一个页面
driver.switchTo().window(WindowsOne);
System.out.println("提交完成: "+timeString);
}
// 退出浏览器
driver.quit();
}
public static void dayList(){//需要生成的考勤月份
Date date1 = new Date();
long d1=date1.getTime();
SimpleDateFormat sdfm = new SimpleDateFormat("yyyy-MM");
String month=sdfm.format(d1);
List<String> times = new ArrayList<String>();
String startDay = month + "-01";
String endDay = nextMonth(month);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentDay = startDay;
Set<String> hoilday = getMonthWekDay(month);
while (true){
try {
if (!(sdf.parse(currentDay).getTime() < sdf.parse(endDay).getTime())) break;
} catch (ParseException e) {
e.printStackTrace();
}
if(!hoilday.contains(currentDay)){
times.add(currentDay + " 09:00");
times.add(currentDay + " 18:00");
}
currentDay = nextDay(currentDay);
currentDay = nextDay(currentDay);
}
stringBuffer.append("[");
for (int i = 0; i < times.size(); i ++){
stringBuffer.append("\"").append(times.get(i)).append("\"");
if(i < times.size()-1){
stringBuffer.append(",");
}
}
stringBuffer.append("]");
System.out.println("时间数组:" + stringBuffer);
days=stringBuffer.substring(1,stringBuffer.length()-1);
}
public static String nextMonth(String month) {
if (month.length() != 7) return "";
Calendar cal = Calendar.getInstance();
String yearStr = month.substring(0, 4);
String monthStr = month.substring(5, 7);
cal.set(Integer.parseInt(yearStr), Integer.parseInt(monthStr) , 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
public static String nextDay(String day) {
if (day.length() != 10) return "";
Calendar cal = Calendar.getInstance();
String yearStr = day.substring(0, 4);
String monthStr = day.substring(5, 7);
String dayStr = day.substring(8,10);
cal.set(Integer.parseInt(yearStr), Integer.parseInt(monthStr)-1 , Integer.parseInt(dayStr) + 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
public static Set<String> getMonthWekDay(String month) {
int year1 = Integer.parseInt(month.substring(0,4));
int month1 = Integer.parseInt(month.substring(5,7));
Set<String> dateList = new HashSet<String>();
SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar(year1, month1-1, 1);
Calendar endCalendar = new GregorianCalendar(year1, month1-1, 1);
endCalendar.add(Calendar.MONTH, 1);
while (true) {
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (weekday == 1 || weekday == 7) {
dateList.add(simdf.format(calendar.getTime()));
}
calendar.add(Calendar.DATE, 1);
if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
break;
}
}
return dateList;
}
}
pom.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ChromeDriver</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>ChromeDriver</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
</properties>
</project>
dayLists.java文件内容:
com.alibaba.fastjson.JSONObject; 这个包可能自动获取不到,需要手动添加文件
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import com.alibaba.fastjson.JSONObject;
public class dayLists {
public static String days;
public static String[] datelist;
public static String dayListString;
public static void main(String[] args) {
init();
}
public static void init(){
test1();
days();
}
public static void days(){
datelist = days.split(",");
System.out.println("datelist "+ Arrays.toString(datelist));
System.out.println("datelist 大小 "+datelist.length);
// System.out.println("本月工作日共有 " + new Integer(datelist.length)/2 +" 天");
System.out.format("本月工作日共有 \33[%d;2m" + new Integer(datelist.length)/2,32);
System.out.format("\33[%d;1m 天",38);
}
public static void test1(){
List<String> times = new ArrayList<>();
String month = "2021-12"; //需要生成的考勤月份
String startDay = month + "-01";
String endDay = nextMonth(month);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentDay = startDay;
Set<String> hoildays = getMonthWekDay(month);
List<String> workDays = new ArrayList<>();
List<String> restDays = new ArrayList<>();
getJjr(month,workDays,restDays);
if (workDays.size() > 0){
for(String workDay:workDays){
hoildays.remove(workDay);
}
}
if (restDays.size() > 0){
for(String restDay:restDays){
hoildays.add(restDay);
}
}
while (true){
try {
if (!(sdf.parse(currentDay).getTime() < sdf.parse(endDay).getTime())) break;
} catch (ParseException e) {
e.printStackTrace();
}
if(!hoildays.contains(currentDay)){
times.add(currentDay + " 09:00");
times.add(currentDay + " 18:00");
}
currentDay = nextDay(currentDay);
}
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("[");
for (int i = 0; i < times.size(); i ++){
stringBuffer.append("\"").append(times.get(i)).append("\"");
if(i < times.size()-1){
stringBuffer.append(",");
}
}
stringBuffer.append("]");
System.out.println("时间数组:\n" + stringBuffer);
dayListString = stringBuffer.toString().substring(1,stringBuffer.length()-1);
System.out.println("时间string:\n" + dayListString+"\n");
days=stringBuffer.substring(1,stringBuffer.length()-1);
}
/**
*
* 下个月1号
*/
public static String nextMonth(String month) {
if (month.length() != 7) return "";
Calendar cal = Calendar.getInstance();
String yearStr = month.substring(0, 4);
String monthStr = month.substring(5, 7);
cal.set(Integer.parseInt(yearStr), Integer.parseInt(monthStr) , 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
/**
*
* 下一天
*/
public static String nextDay(String day) {
if (day.length() != 10) return "";
Calendar cal = Calendar.getInstance();
String yearStr = day.substring(0, 4);
String monthStr = day.substring(5, 7);
String dayStr = day.substring(8,10);
cal.set(Integer.parseInt(yearStr), Integer.parseInt(monthStr)-1 , Integer.parseInt(dayStr) + 1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
/**
*
* 本月的周末
*/
public static Set<String> getMonthWekDay(String month) {
int year1 = Integer.parseInt(month.substring(0,4));
int month1 = Integer.parseInt(month.substring(5,7));
Set<String> dateList = new HashSet<>();
SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar(year1, month1-1, 1);
Calendar endCalendar = new GregorianCalendar(year1, month1-1, 1);
endCalendar.add(Calendar.MONTH, 1);
while (true) {
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (weekday == 1 || weekday == 7) {
dateList.add(simdf.format(calendar.getTime()));
}
calendar.add(Calendar.DATE, 1);
if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
break;
}
}
return dateList;
}
/**
* 获取节假日+补班日,不含周末
*
*/
private static void getJjr (String month,List<String> workDays,List<String> restDays) {
String url = "http://timor.tech/api/holiday/year/" + month;
OkHttpClient client = new OkHttpClient();
Response response;
//解密数据
String rsa = null;
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
try {
response = client.newCall(request).execute();
rsa = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
Map map = JSONObject.parseObject(rsa, Map.class);
//遍历获取节假日和补班日
if(map != null){
JSONObject holidayJson = (JSONObject) map.get("holiday");
Set<Map.Entry<String,Object>> holidays = holidayJson.entrySet();
if(holidays.size() > 0){
for(Map.Entry<String,Object> holiday : holidays){
String key = holiday.getKey();
Map<String,Object> value = (Map<String,Object>)holiday.getValue();
Boolean rest = (Boolean) value.get("holiday");
if(rest){
restDays.add(String.valueOf(value.get("date")));
}else{
workDays.add(String.valueOf(value.get("date")));
}
}
}
}
}
}