初次代码提交

This commit is contained in:
fangl 2024-02-20 10:03:31 +08:00 committed by fl
commit fdced77494
4 changed files with 440 additions and 0 deletions

View File

@ -0,0 +1,261 @@
package com.bonus.autoweb;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.concurrent.TimeUnit;
/**
* 自动化操作
*
* @author 吕继龙
*/
public class AutoMain {
//登录的次数
private int loginNum = 20;
//浏览器驱动对象
private WebDriver webDriver;
/**
* 初始化驱动
*/
public void initDrive() {
System.out.println("自动评价开始------------");
// chromedriver服务地址
System.setProperty(DataConfig.DRIVE_NAME, DataConfig.drivePath);
ChromeOptions option = new ChromeOptions();
option.addArguments("disable-infobars");
option.setBinary("D:\\auto_handle\\Chrome-bin\\chrome.exe");
// option.addArguments("--headless"); //隐藏页面
webDriver = new ChromeDriver(option);
}
/**
* 开始工作
*/
public void startAuto() throws Exception {
login();
if (loginNum <= 0) {
closeDrive();
return;
} else {
try {
autoHandle();
} catch (Exception e) {
e.printStackTrace();
startAuto();
}
}
}
/**
* 模拟评价
*/
private void autoHandle() throws Exception {
Thread.sleep(45*1000);
openMyAppraise();
startAppraise();
closeDrive();
}
/**
* 打开网站
*/
public void openWeb() throws InterruptedException {
//第一步打开指定网站
webDriver.get(DataConfig.URL); // 打开指定的网站
System.out.println("打开订单评价网站------------");
Thread.sleep(2000);
}
/**
* 打开网站并登录系统
*/
private void login() throws InterruptedException {
//******************************************************************
/**
* 第二步 用户登录
*/
try {
//角色选择
webDriver.findElement(By.id("userType")).click();
Thread.sleep(600);
webDriver.findElement(By.xpath("//*[@id=\"form_submit\"]/ul/li[1]/div/div/ul/li[1]")).click();
System.out.println("选择角色------------");
Thread.sleep(300);
//输入用户名
webDriver.findElement(By.id("username")).sendKeys(DataConfig.USER_NAME);
System.out.println("输入用户名------------");
Thread.sleep(300);
//输入密码
webDriver.findElement(By.id("password")).sendKeys(DataConfig.PASS);
System.out.println("输入密码------------");
Thread.sleep(300);
loginNum--;
}catch(NoSuchWindowException e){
System.out.println(e);
closeDrive();
}
//***********验证码识别start*****************
// verCodeHandler();
// Thread.sleep(30000);
// webDriver.findElement(By.className("login-btn")).click();
}
//结束进程
public void endProcess() {
// try{
// String[] cmd = { "自动操作助手.exe"};
// Process proc = Runtime.getRuntime().exec(cmd);//开启文本
// BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
// String string_Temp = in.readLine();
// while (string_Temp != null)
// {
// System.out.println(string_Temp);
// if(string_Temp.contains("自动操作助手.exe"))
// Runtime.getRuntime().exec("Taskkill /IM 自动操作助手.exe");
// string_Temp = in.readLine();
// }
// }catch (Exception e){
// //结束进程
// }
}
/**
* 验证码操作
*/
private void verCodeHandler() throws Exception {
System.out.println("验证码验证开始------------");
loginNum--;
/**
* 循环十次成功退出
*/
int i;
for (i = 0; i < 10; i++) {
String imageUrl = webDriver.findElement(By.id("personSecurityCode")).getAttribute("src");
Thread.sleep(500);
String verCode = VerCode.verCodeHandler(imageUrl);
System.out.println("验证码识别结果:" + verCode);
if (StringUtils.isEmpty(verCode)) {
System.out.println("" + (i + 1) + "次验证失败!");
continue;
} else {
System.out.println("" + (i + 1) + "次验证成功!");
Thread.sleep(300);
webDriver.findElement(By.id("person_reg_security")).sendKeys(verCode);
Thread.sleep(300);
break;
}
}
if (i < 10) {
//点击登录
System.out.println("登录系统------------");
webDriver.findElement(By.className("login-btn")).click();
Thread.sleep(3000);
try {
webDriver.findElement(By.id("to_seach_id")).sendKeys("你好");
Thread.sleep(300);
} catch (Exception e) {
verCodeHandler();
}
} else {
verCodeHandler();
}
}
/**
* 第三步 打开我的评价页面
*
* @throws InterruptedException
*/
private void openMyAppraise() throws InterruptedException {
webDriver.get("http://buser.esgcc.com.cn/order/list"); // 打开指定的网站
System.out.println("打开订单页面------------");
webDriver.navigate().refresh();
Thread.sleep(300);
//打开我的评价
webDriver.findElement(By.id("ctl_menu_viewhis")).click();
System.out.println("点击我的评价------------");
Thread.sleep(3000);
try {
webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
} finally {
// webDriver.quit();
}
Thread.sleep(3000);
}
/**
* 开始评价工作
*/
private void startAppraise() throws InterruptedException {
/**
* 第四步 获取所有未评价数据并进行评价
*/
//获取所有的评价标签
List<WebElement> list = webDriver.findElements(By.className("btn-sm-one"));
System.out.println("开始获取未评价的列表------------数量为:"+(list.size()-1));
for (int i = 0; i < list.size(); i++) {
WebElement element = list.get(i);
String path = element.getAttribute("href");
System.out.println(path);
if (StringUtils.isEmpty(path)) {
list.remove(i);
i--;
continue;
} else {
//打开订单评价的页面
//属性重新赋值
JavascriptExecutor jss = (JavascriptExecutor) webDriver;
jss.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", element, "target", "");
element.click();
Thread.sleep(300);
//评价
Random random = new Random(5);
int m = random.nextInt(5);
webDriver.findElement(By.id("evaluate_content1")).sendKeys(DataConfig.handleContent[m]);
Thread.sleep(300);
//提交评价
webDriver.findElement(By.className("submit_btn")).click();
Thread.sleep(300);
webDriver.navigate().back();
Thread.sleep(300);
webDriver.navigate().refresh();
Thread.sleep(300);
list = webDriver.findElements(By.className("btn-sm-one"));
Thread.sleep(300);
i--;
}
}
}
/**
* 关闭浏览器驱动
*/
private void closeDrive() {
System.out.println("工作完成,退出浏览器");
webDriver.quit();
endProcess();
}
}

View File

@ -0,0 +1,48 @@
package com.bonus.autoweb;
public class DataConfig {
/**
* url 网站地址
*/
protected static final String URL = "http://b.esgcc.com.cn/";
/**
* 驱动名称
*/
protected static final String DRIVE_NAME = "webdriver.chrome.driver";
/**
* 用户名
*/
protected static final String USER_NAME = "18255755085";
/**
* 密码
*/
protected static final String PASS = "zk@3073825";
/**
* 浏览器驱动地址
*/
protected static final String drivePath = "D:\\auto_handle\\chromedriver.exe";
/**
* 验证码识别接口地址
*/
protected static final String OCR_URL="http://localhost:7011/ocr";
/**
* 验证码图片下载地址
*/
protected static final String verCodePath = "E:\\vercodeTest\\";
/**
* 评价内容
*/
protected static final String[] handleContent = {"产品还可以。","供应商服务态度好。","能够按时发货。服务好",
"外观无破损,服务、质量较好。","质量很好,有需要下次还会回购!"};
}

View File

@ -0,0 +1,19 @@
package com.bonus.autoweb;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestMain {
public static void main(String[] args) throws Exception {
AutoMain autoMain=new AutoMain();
autoMain.initDrive();
autoMain.openWeb();
autoMain.startAuto();
}
}

View File

@ -0,0 +1,112 @@
package com.bonus.autoweb;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 验证码识别
*/
public class VerCode {
/**
* 验证码识别
*
* @param imageUrl
*/
public static String verCodeHandler(String imageUrl) throws Exception {
//下载验证码
String filePath = downLoadVerCodeImage(imageUrl);
if (StringUtils.isEmpty(filePath)) {
return null;
} else {
String result = vercodeDesc(filePath);
if (StringUtils.isEmpty(result) || result.length() != 4) {
return null;
} else {
return result;
}
}
}
/**
* 验证码识别
*
* @return
*/
private static String vercodeDesc(String filePath) {
File f=new File(filePath);
byte[] bytes = FileUtil.readBytes(f);
String result = HttpUtil.createPost(DataConfig.OCR_URL).body(bytes).execute().body();
if("-1".equals(result)){
return null;
}
return result;
}
/**
* 下载验证码图片
*
* @return
*/
private static String downLoadVerCodeImage(String imagePath) throws Exception {
//定义一个URL对象就是你想下载的图片的URL地址
URL url = new URL(imagePath);
//打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为10秒
conn.setConnectTimeout(10 * 1000);
//通过输入流获取图片数据
InputStream is = conn.getInputStream();
//得到图片的二进制数据以二进制封装得到数据具有通用性
byte[] data = readInputStream(is);
//创建一个文件对象用来保存图片默认保存当前工程根目录起名叫Copy.jpg
String filePath = DataConfig.verCodePath + "verCode.jpg";
File imageFile = new File(filePath);
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流释放资源
outStream.close();
return filePath;
}
private static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//创建一个Buffer字符串
byte[] buffer = new byte[6024];
//每次读取的字符串长度如果为-1代表全部读取完毕
int len;
//使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
//用输出流往buffer里写入数据中间参数代表从哪个位置开始读len代表读取的长度
outStream.write(buffer, 0, len);
}
//关闭输入流
inStream.close();
//把outStream里的数据写入内存
return outStream.toByteArray();
}
public static void main( String[] args ) {
File dir = new File("E:\\vercodeTest");
for(File f:dir.listFiles()) {
byte[] bytes = FileUtil.readBytes(f);
String result = HttpUtil.createPost("http://localhost:7011/ocr").body(bytes).execute().body();
FileUtil.copy(f, new File("E:\\vercodeTest\\vercodeTest4\\"+result+".jpg"), true);
}
}
}