Springboot集成selemium+tess4j+opencv 实现验证码识别,去干扰线,模拟用户登录 ( 一 )

最近新入职了一家公司,果然一进去就给了一个很大的坑。模拟用户登录XX网站 。这是要玩图像识别的节奏啊。。。吓尿了 赶紧度娘一波。

Selemium

1 :引入 maven

`

    
        org.sele~~~~niumhq.selenium
        selenium-java
        3.141.0
    
    
        org.seleniumhq.selenium
        selenium-server
        3.141.0
    
    ~~~~
        org.seleniumhq.selenium
        selenium-chrome-driver
        2.41.0
    `
    

2 :引入chrome插件 chromedriver.exe

其中chromedriver.exe可以在网址http://chromedriver.storage.g... 注意版本问题, 查看本地chrom版本并下载对应的chromedriver.exe, 版本对应可在版本明细中的notes.txt页面查看

3 :封装SelemiumUtil工具类

public class SeleniumUtil {

    private static final ChromeOptions chromeOptions = new ChromeOptions();

    static {
        System.setProperty("webdriver.chrome.driver", "src\\main\\resources\\chromedriver.exe");
        Map chromePrefs = CollUtil.newHashMap();
        //禁止弹窗
        chromePrefs.put("profile.default_content_settings.popups", 0);
        //下载地址
//        chromePrefs.put("download.default_directory", "C://xx//");
        //禁止图片加载
//        chromePrefs.put("profile.managed_default_content_settings.images", 2);
        //userAgent=ie11
        String userAgentIE11="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36";
        chromePrefs.put("profile.general_useragent_override", userAgentIE11);

        HashMap mobileEmulation = new HashMap();
        //用iPhone X 屏幕启动
//        mobileEmulation.put("deviceName","iPhone X");

        chromeOptions.setExperimentalOption("prefs",chromePrefs);
        chromeOptions.setExperimentalOption("mobileEmulation",mobileEmulation);
        /***********************************以下设置启动参数******************************************/
        //消除安全校验
        chromeOptions.addArguments("--allow-running-insecure-content");
        //启动最大化,防止失去焦点
        chromeOptions.addArguments("--start-maximized");
        //关闭gpu图片渲染
        chromeOptions.addArguments("--disable-gpu");
    }

    /**
     * 获取浏览器对象直接操作
     * @return
     */
    public static WebDriver build(ChromeOptions chromeOptions) {
        WebDriver dr =  new ChromeDriver(chromeOptions);
        dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return dr;
    }

    /**
     * 获取浏览器对象直接操作
     * @return
     */
    public static WebDriver build() {
        return SeleniumUtil.build(chromeOptions);
    }

    public static final String SOCIAL_INSURANCE_URL = "自定义需要模拟的网址";

    /**
     * 查询页面元素并设置值
     * @param driver
     * @param szsbEnum
     * @param var1
     */
    public static void findAndSave(WebDriver driver,SzsbEnum szsbEnum,CharSequence... var1){
        WebElement element ;
        if ((element = findElement(driver,szsbEnum))!= null) element.sendKeys(var1);
    }

    /**
     * 搜索页面元素并且点击
     * @param driver
     * @param szsbEnum
     */
    public static void findAndClick(WebDriver driver,SzsbEnum szsbEnum){
        findElement(driver,szsbEnum).click();
    }

    /**
     * 搜索页面元素并且点击
     * @param driver
     * @param szsbEnum
     */
    public static void findAndClick(WebDriver driver,SzsbEnum szsbEnum,ThreadLocal> threadLocal){
        Integer integer = threadLocal.get().get(szsbEnum);
        if (integer == null){
            findElement(driver,szsbEnum).click();
        }else {
            findElement(driver,szsbEnum,integer).click();
        }
    }

    /**
     * 搜索页面元素并且点击
     * @param driver
     * @param szsbEnum
     */
    public static void findAndClick(WebDriver driver,SzsbEnum szsbEnum,int wait){
        findElement(driver,szsbEnum,wait).click();
    }

    /**
     * 查询单个页面元素
     * @param driver
     * @param szsbEnum
     * @param wait 等待因子
     * @return
     */
    public static WebElement findElement(WebDriver driver,SzsbEnum szsbEnum,int wait){
        WebElement element = null;
        int a = 0;
        By[] bys = szsbEnum.getBy();
        for (By by : bys) {
            if (a > bys.length*3) break;
            //判断元素是否可用
            WebElement element1 = null;
            try {
                element1 = driver.findElement(by);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (element1!=null && element1.isDisplayed() && element1.isEnabled()){
                element = element1;
            }else {
                a++;
                try {
                    Thread.sleep(wait*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        if (element == null) throw new SelemiumNotFoundException("Selenium cannot find element : " + szsbEnum.by,szsbEnum);
        return element;
    }

    /**
     * 查询单个页面元素
     * @param driver
     * @param szsbEnum
     * @return
     */
    public static WebElement findElement(WebDriver driver,SzsbEnum szsbEnum){
        return findElement(driver,szsbEnum,1);
    }

    /**
     * 查询页面多个页面元素
     * @param driver
     * @param szsbEnum
     * @return
     */
    public static List findElements(WebDriver driver,SzsbEnum szsbEnum){
        return driver.findElements(szsbEnum.getBy()[0]);
    }

    public enum SzsbEnum{
        /* login */
        USER_NAME_INPUT_SELECT(By.id("userNameInput")),
        LOGIN_YZM_ID(By.id("yzm")),
        USER_PWD_INPUT_SELECT(By.id("userPwdInput")),
        INFO_ALERT(By.className("dw-res-wnd-header-closebtn-ext")),
//        INFO_ALERT(By.id("dw_button_f3e0d7a3_a373_4df1_b1ae_25ee3adad2d3")),
        LOGIN_BUTTON(By.xpath("//div[@id='f_cont']/div[1]/div[1]/div[1]/form/div[6]/div[1]")),


        INDEX_CBDJGL(By.id("returnYwsb('F02000')")),
        INDEX_RYPLCB(By.id("grcbdjgl-p2")),
        INDEX_RYPLTJ(By.id("grcbdjgl-p4")),

        INDEX_STOP_DRBPWJ(By.xpath("//div[contains(string(),'导入报盘文件')]")),
        INDEX_STOP_DRBPWJ_NEXT(By.name("btn_goNext")),

        INDEX_FILEUPLOAD(By.name("fileupload")),
//        INDEX_FORM(By.id("__dw_sform_form_1b997ea4_2600_4c95_bd35_59883bc7514a_form")),
        INDEX_FILEUPLOAD_BTN(By.name("btnUpload")),

        LOGIN_VALIDATA_INPUT(By.id("validatecode1"));
        private By[] by;
        SzsbEnum(By... by){
            this.by = by;
        }
        public By[] getBy(){
            return this.by;
        }
    }
}

Tess4j OCR图像识别框架集成

1:引入maven

`

    
        net.java.dev.jna
        jna
        5.1.0
    
    
        net.sourceforge.tess4j
        tess4j
        4.3.1
        
            
                com.sun.jna
                jna
            
        
    

`

2:导入训练库

训练库下载地址 : https://sourceforge.net/projects/tess4j/files/

下载完之后解压 将 tessdata 复制到resource目录下

3:初始化 tess4j

Opencv 集成

1:下载opencv

下载地址 :https://sourceforge.net/projects/opencvlibrary/files/4.1.1/opencv-4.1.1-vc14_vc15.exe/download
下载完是一个.exe 文件 直接安装之后会生成一个目录

将jar包引入项目
`


        org.opencv
        opencv
        4.1.1
        system
        ${project.basedir}/lib/opencv-411.jar
    `

还没完 将刚才目录下的 x64 目录下有个opencv_java411.dll文件 ,放到项目的resource目录下,然后在项目初始化中引入
`

    String opencvLib = System.getProperty("user.dir") + "\\src\\main\\resources\\opencv";
    addLibraryDir(opencvLib);
    System.load(opencvLib+"\\opencv_java411.dll");
    

`
addLibraryDir 代码也贴一下

public void addLibraryDir(String libraryPath) throws IOException {
        try {
            Field field = ClassLoader.class.getDeclaredField("usr_paths");
            field.setAccessible(true);
            String[] paths = (String[]) field.get(null);
            for (int i = 0; i < paths.length; i++) {
                if (libraryPath.equals(paths[i])) {
                    return;
                }
            }

            String[] tmp = new String[paths.length + 1];
            System.arraycopy(paths, 0, tmp, 0, paths.length);
            tmp[paths.length] = libraryPath;
            field.set(null, tmp);
        } catch (IllegalAccessException e) {
            throw new IOException(
                    "Failedto get permissions to set library path");
        } catch (NoSuchFieldException e) {
            throw new IOException(
                    "Failedto get field handle to set library path");
        }
    }

好了,到这边算是初始化完成。图像识别的代码下一章贴一下,不然一章东西太多写不下。

你可能感兴趣的