selenium操作易盾验证码
易盾验证码模拟
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@Slf4j
public class YidunRPA {
public static void main(String[] args) {
Long start = System.currentTimeMillis();
for (int i=0;i<100;i++){
login();
log.info("第{}次登陆,耗时{}秒",(i+1),(System.currentTimeMillis()-start)/1000);
}
}
public static void login(){
System.setProperty("webdriver.chrome.driver","C:\\windows\\chromedriver.exe");
//创建无Chrome无头参数
ChromeOptions chromeOptions=new ChromeOptions();
//chromeOptions.addArguments("-headless");
WebDriver browser = new ChromeDriver(chromeOptions);
browser.get("https://ssfw.gdcourts.gov.cn/web/loginA");
WebElement loginWE = browser.findElement(By.id("login_user_name"));
loginWE.sendKeys("151111111111");
WebElement psdWE = browser.findElement(By.id("psw"));
psdWE.sendKeys("1111111");
JavascriptExecutor jse = (JavascriptExecutor)browser;
jse.executeScript("document.getElementById(\"psw\").value=\"pwd\";");
jse.executeScript("document.getElementById(\"login_pwdkey_txt\").value=\"pwd\";");
for (int count=0;count<10;count++){
log.info("验证码重试次数{}",(count+1));
if (captcha(browser)){
break;
}
}
System.out.println("end");
}
public static boolean captcha(WebDriver browser){
//需要UI界面,有头模式
Actions action=new Actions(browser);
action.moveToElement(browser.findElement(By.id("captcha")));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//等待图片出现
WebElement bgimgWE = (new WebDriverWait( browser, 3)).until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d){
return d.findElement(By.className("yidun_bg-img"));
}
}
);
String bgimgUrl = bgimgWE.getAttribute("src");
WebElement jigsawWE = browser.findElement(By.className("yidun_jigsaw"));
String jigsawUrl = jigsawWE.getAttribute("src");
double distance = getDistance(bgimgUrl,jigsawUrl);
try {
move(browser,browser.findElement(By.className("yidun_slider")),Integer.parseInt(new java.text.DecimalFormat("0").format(distance)));
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement element =null;
try {
//等待元素出现
element = (new WebDriverWait( browser, 5)).until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d){
return d.findElement(By.xpath("//*[@id=\"page-wrapper\"]/div[2]/nav/div/a"));
}
}
);
log.info(element.getText());
browser.quit();
return true;
} catch (Exception e) {
log.info("获取元素失败");
return false;
}
}
/**
* 获取网易验证滑动距离
*
* @return
*/
public static String dllPath = "E:\\CODE\\rpa\\opencv\\opencv_java451.dll";
public static double getDistance(String bUrl, String sUrl) {
log.info(bUrl);
log.info(sUrl);
System.load(dllPath);
File bFile = new File("E:/EasyDun_b.png");
File sFile = new File("E:/EasyDun_s.png");
try {
FileUtils.copyURLToFile(new URL(bUrl), bFile);
BufferedImage bgBI = ImageIO.read(bFile);
FileUtils.copyURLToFile(new URL(sUrl), sFile);
BufferedImage sBI = ImageIO.read(sFile);
// 裁剪
cropImage(bgBI, sBI, bFile, sFile);
Mat s_mat = Imgcodecs.imread(sFile.getPath());
Mat b_mat = Imgcodecs.imread(bFile.getPath());
//阴影部分为黑底时需要转灰度和二值化,为白底时不需要
// 转灰度图像
Mat s_newMat = new Mat();
Imgproc.cvtColor(s_mat, s_newMat, Imgproc.COLOR_BGR2GRAY);
// 二值化图像
binaryzation(s_newMat);
Imgcodecs.imwrite(sFile.getPath(), s_newMat);
int result_rows = b_mat.rows() - s_mat.rows() + 1;
int result_cols = b_mat.cols() - s_mat.cols() + 1;
Mat g_result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
Imgproc.matchTemplate(b_mat, s_mat, g_result, Imgproc.TM_SQDIFF);
// 归一化平方差匹配法TM_SQDIFF 相关系数匹配法TM_CCOEFF
Core.normalize(g_result, g_result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
Point matchLocation = new Point();
Core.MinMaxLocResult mmlr = Core.minMaxLoc(g_result);
matchLocation = mmlr.maxLoc;
// 此处使用maxLoc还是minLoc取决于使用的匹配算法
Imgproc.rectangle(b_mat, matchLocation, new Point(matchLocation.x + s_mat.cols(), matchLocation.y + s_mat.rows()), new Scalar(0, 255, 0, 0));
Imgcodecs.imwrite(bFile.getPath(), b_mat);
return matchLocation.x + s_mat.cols() - sBI.getWidth() + 12;
} catch (Throwable e) {
e.printStackTrace();
return 0;
} finally {
// bFile.delete();
// sFile.delete();
}
}
/**
* 图片亮度调整
*
* @param image
* @param param
* @throws IOException
*/
public static void bloding(BufferedImage image, int param) throws IOException {
if (image == null) {
return;
} else {
int rgb, R, G, B;
for (int i = 0; i < image.getWidth(); i++) {
for (int j = 0; j < image.getHeight(); j++) {
rgb = image.getRGB(i, j);
R = ((rgb >> 16) & 0xff) - param;
G = ((rgb >> 8) & 0xff) - param;
B = (rgb & 0xff) - param;
rgb = ((clamp(255) & 0xff) << 24) | ((clamp(R) & 0xff) << 16) | ((clamp(G) & 0xff) << 8) | ((clamp(B) & 0xff));
image.setRGB(i, j, rgb);
}
}
}
}
// 判断a,r,g,b值,大于256返回256,小于0则返回0,0到256之间则直接返回原始值
private static int clamp(int rgb) {
if (rgb > 255){
return 255;}
if (rgb < 0){
return 0;}
return rgb;
}
/**
* 生成半透明小图并裁剪
*
* @param image
* @return
*/
private static void cropImage(BufferedImage bigImage, BufferedImage smallImage, File bigFile, File smallFile) {
int y = 0;
int h_ = 0;
try {
// 2 生成半透明图片
bloding(bigImage, 75);
for (int w = 0; w < smallImage.getWidth(); w++) {
for (int h = smallImage.getHeight() - 2; h >= 0; h--) {
int rgb = smallImage.getRGB(w, h);
int A = (rgb & 0xFF000000) >>> 24;
if (A >= 100) {
rgb = (127 << 24) | (rgb & 0x00ffffff);
smallImage.setRGB(w, h, rgb);
}
}
}
for (int h = 1; h < smallImage.getHeight(); h++) {
for (int w = 1; w < smallImage.getWidth(); w++) {
int rgb = smallImage.getRGB(w, h);
int A = (rgb & 0xFF000000) >>> 24;
if (A > 0) {
if (y == 0){
y = h;}
h_ = h - y;
break;
}
}
}
smallImage = smallImage.getSubimage(0, y, smallImage.getWidth(), h_);
bigImage = bigImage.getSubimage(0, y, bigImage.getWidth(), h_);
ImageIO.write(bigImage, "png", bigFile);
ImageIO.write(smallImage, "png", smallFile);
} catch (Throwable e) {
System.out.println(e.toString());
}
}
/**
*
* @param mat
* 二值化图像
*/
public static void binaryzation(Mat mat) {
int BLACK = 0;
int WHITE = 255;
int ucThre = 0, ucThre_new = 127;
int nBack_count, nData_count;
int nBack_sum, nData_sum;
int nValue;
int i, j;
int width = mat.width(), height = mat.height();
// 寻找最佳的阙值
while (ucThre != ucThre_new) {
nBack_sum = nData_sum = 0;
nBack_count = nData_count = 0;
for (j = 0; j < height; ++j) {
for (i = 0; i < width; i++) {
nValue = (int) mat.get(j, i)[0];
if (nValue > ucThre_new) {
nBack_sum += nValue;
nBack_count++;
} else {
nData_sum += nValue;
nData_count++;
}
}
}
nBack_sum = nBack_sum / nBack_count;
nData_sum = nData_sum / nData_count;
ucThre = ucThre_new;
ucThre_new = (nBack_sum + nData_sum) / 2;
}
// 二值化处理
int nBlack = 0;
int nWhite = 0;
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
nValue = (int) mat.get(j, i)[0];
if (nValue > ucThre_new) {
mat.put(j, i, WHITE);
nWhite++;
} else {
mat.put(j, i, BLACK);
nBlack++;
}
}
}
// 确保白底黑字
if (nBlack > nWhite) {
for (j = 0; j < height; ++j) {
for (i = 0; i < width; ++i) {
nValue = (int) (mat.get(j, i)[0]);
if (nValue == 0) {
mat.put(j, i, WHITE);
} else {
mat.put(j, i, BLACK);
}
}
}
}
}
// 延时加载
private static WebElement waitWebElement(WebDriver driver, By by, int count) throws Exception {
WebElement webElement = null;
boolean isWait = false;
for (int k = 0; k < count; k++) {
try {
webElement = driver.findElement(by);
if (isWait) {
System.out.println(" ok!");
}
return webElement;
} catch (org.openqa.selenium.NoSuchElementException ex) {
isWait = true;
if (k == 0) {
System.out.print("waitWebElement(" + by.toString() + ")");
}else {
System.out.print(".");
}
Thread.sleep(50);
}
}
if (isWait) {
System.out.println(" outTime!");
}
return null;
}
/**
* 模拟人工移动
* @param driver
* @param element页面滑块
* @param distance需要移动距离
*/
public static void move(WebDriver driver, WebElement element, int distance) throws InterruptedException {
int randomTime = 0;
if (distance > 90) {
randomTime = 250;
} else if (distance > 80 && distance <= 90) {
randomTime = 150;
}
List<Integer> track = getMoveTrack(distance - 2);
int moveY = 1;
try {
Actions actions = new Actions(driver);
actions.clickAndHold(element).perform();
//Thread.sleep(200);
for (int i = 0; i < track.size(); i++) {
actions.moveByOffset(track.get(i), moveY).perform();
Thread.sleep(new Random().nextInt(300) + randomTime);
}
Thread.sleep(200);
actions.release(element).perform();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("1");
}
/**
* 根据距离获取滑动轨迹
* @param distance需要移动的距离
* @return
*/
public static List<Integer> getMoveTrack(int distance) {
List<Integer> track = new ArrayList<>();// 移动轨迹
Random random = new Random();
int current = 0;// 已经移动的距离
int mid = (int) distance * 4 / 5;// 减速阈值
int a = 0;
int move = 0;// 每次循环移动的距离
while (true) {
a = random.nextInt(10);
if (current <= mid) {
move += a;// 不断加速
} else {
move -= a;
}
if ((current + move) < distance) {
track.add(move);
} else {
track.add(distance - current);
break;
}
current += move;
}
return track;
}
/**
* 计算滑动距离
* @param bUrl
* @param sUrl
* @return
*/
public static double getSlideDistance(String bUrl, String sUrl){
try {
File bFile = new File("E:/EasyDun1_b.png");
File sFile = new File("E:/EasyDun1_s.png");
// 加载OpenCV本地库
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.load(dllPath);
FileUtils.copyURLToFile(new URL(bUrl), bFile);
BufferedImage bgBI = ImageIO.read(bFile);
FileUtils.copyURLToFile(new URL(sUrl), sFile);
BufferedImage sBI = ImageIO.read(sFile);
// 裁剪
// cropImage(bgBI, sBI, bFile, sFile);
//对滑块进行处理
Mat slideBlockMat = Imgcodecs.imread(sFile.getPath());
//1、灰度化图片
Imgproc.cvtColor(slideBlockMat, slideBlockMat, Imgproc.COLOR_BGR2GRAY);
//2、去除周围黑边
for (int row = 0; row < slideBlockMat.height(); row++) {
for (int col = 0; col < slideBlockMat.width(); col++) {
if (slideBlockMat.get(row, col)[0] == 0) {
slideBlockMat.put(row, col, 96);
}
}
}
//3、inRange二值化转黑白图
Core.inRange(slideBlockMat, Scalar.all(96), Scalar.all(96), slideBlockMat);
Imgcodecs.imwrite(sFile.getPath(), slideBlockMat);
//对滑动背景图进行处理
Mat slideBgMat = Imgcodecs.imread(bFile.getPath());
//1、灰度化图片
Imgproc.cvtColor(slideBgMat, slideBgMat, Imgproc.COLOR_BGR2GRAY);
//2、二值化
Imgproc.threshold(slideBgMat, slideBgMat, 127, 255, Imgproc.THRESH_BINARY);
Imgcodecs.imwrite(bFile.getPath(), slideBgMat);
Mat g_result = new Mat();
/*
* matchTemplate:在模板和输入图像之间寻找匹配,获得匹配结果图像
* result:保存匹配的结果矩阵
* TM_CCOEFF_NORMED标准相关匹配算法
*/
Imgproc.matchTemplate(slideBgMat, slideBlockMat, g_result, Imgproc.TM_CCOEFF_NORMED);
/* minMaxLoc:在给定的结果矩阵中寻找最大和最小值,并给出它们的位置
* maxLoc最大值
*/
Point matchLocation = Core.minMaxLoc(g_result).maxLoc;
//返回匹配点的横向距离
return matchLocation.x;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}
opencv动态链接库以及jar下载地址:
链接: https://pan.baidu.com/s/128Efmj_UqlbPUx7v_pW9DA 提取码: andx
注意:有一个问题还没有解决,还无法区分阴影部分是黑色还是白色。 因为两种的情况不同,所以处理方式也不同。阴影部分为黑底时需要转灰度和二值化,为白底时不需要。黑底使用归一化平方差匹配算法 TM_SQDIFF ,而白底使用相关系数匹配算法 TM_CCOEFF。
因以上关系,无法区分阴影部分是黑色还是白色,顾存在失败的几率,在程序中通过重试登录进行处理,经测试大概率在3次以内能够登录成功。
参考:https://blog.csdn.net/weixin_44549063/article/details/112193516