Selenium 识别验证码并自动登录

发布于:2025-06-14 ⋅ 阅读:(16) ⋅ 点赞:(0)
		<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.1.0</version>
        </dependency>
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ChromeUtil {

    @Value("${spring.profiles.active}")
    private String active;

    /**
     * 根据环境获取驱动
     */
    public WebDriver getDriver() {
        if (StrUtil.equals(active, "test") && SystemUtil.getOsInfo().isWindows()) {
            return getDriverWindows();
        }
        if (StrUtil.equals(active, "prod") && SystemUtil.getOsInfo().isWindows()) {
            return getDriverWindows();
        }
        if (StrUtil.equals(active, "prod") && SystemUtil.getOsInfo().isLinux()) {
            return getDriverLinux();
        }

        return getDriverWindows();
    }

    /**
     * 获取本地谷歌驱动
     */
    public WebDriver getDriverWindows() {
        //获取驱动路径
        System.setProperty("webdriver.chrome.driver", "D:\\data\\static\\driver\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");

        return new ChromeDriver(chromeOptions);
    }

    /**
     * 获取服务器谷歌驱动
     */
    public WebDriver getDriverLinux() {
        //获取驱动路径
        System.setProperty("webdriver.chrome.driver", "/data/static/chromedriver");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--no-sandbox");//禁用沙箱
        chromeOptions.addArguments("--disable-dev-shm-usage");//禁用开发者shm

        chromeOptions.addArguments("window-size=1920x3000"); //指定浏览器分辨率
        chromeOptions.addArguments("--disable-gpu"); //谷歌文档提到需要加上这个属性来规避bug
        chromeOptions.addArguments("--hide-scrollbars"); //隐藏滚动条, 应对一些特殊页面
        chromeOptions.addArguments("blinfk-settings=imagesEnabled=alse"); //不加载图片, 提升速度
        chromeOptions.addArguments("--headless"); //浏览器不提供可视化页面.linux下如果系统不支持可视化不加这条会启动失败

        return new ChromeDriver(chromeOptions);
    }

}

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.la.selenium.utils.CaptchaImageUtil;
import com.la.selenium.utils.ChromeUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class YqtLoginService {

    @Autowired
    private ChromeUtil chromeUtil;

    @Value("${yqt.url:https://yqt.xxx.com}")
    private String yqtUrl;

    @Value("${yqt.username:xxx}")
    private String yqtUsername;

    @Value("${yqt.password:xxx}")
    private String yqtPassword;

    public WebDriver login() {
        WebDriver driver = chromeUtil.getDriver();
        driver.manage().window().maximize();
        driver.get(yqtUrl);

        WebElement captchaImage = driver.findElement(By.xpath("/html/body/app-root/app-oem/app-oem/main/div[1]/div/form/nz-form-item[3]/nz-form-control/div/div/nz-input-group/span/img"));
        String captchaImageUrl = captchaImage.getAttribute("src");
        log.info("获取验证码Url:{}", captchaImageUrl);

        String base64Image = captchaImage.getScreenshotAs(OutputType.BASE64);
        log.info("获取验证码Base64:{}", base64Image);

        // OCR 获取验证码
        String result = CaptchaImageUtil.getCaptcha(base64Image);

        // 输入账号 密码 验证码
        WebElement usernameInput = driver.findElement(By.cssSelector("input[formcontrolname='userName']"));
        usernameInput.sendKeys(yqtUsername);
        WebElement passwordInput = driver.findElement(By.cssSelector("input[formcontrolname='password']"));
        passwordInput.sendKeys(yqtPassword);
        WebElement captchaInput = driver.findElement(By.cssSelector("input[formcontrolname='yqzcode']"));
        captchaInput.sendKeys(result);

        // 延时
        ThreadUtil.sleep(RandomUtil.randomInt(1000));

        // 点击登录
        WebElement loginButton = driver.findElement(By.xpath("//button//span[contains(text(),'登录')]"));
        loginButton.click();

        return driver;
    }
}

package com.la.selenium.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 服务搭建
 * https://blog.csdn.net/z1353095373/article/details/147759264
 *
 * @author jason
 */
@Slf4j
public class CaptchaImageUtil {

    /**
     * OCR识别验证码
     * 
     * image: xzxxn777/ddddocr:latest
     */
    public static String getCaptcha(String base64Image) {
        String url = "http://xxx:7777/classification";

        JSONObject request = new JSONObject();
        request.set("image", base64Image);
        String response = HttpUtil.post(url, request.toString());
        String result = JSONUtil.parseObj(response).getStr("result");

        log.info("OCR识别验证码:{}", result);

        return result;
    }

}