使用java + selenium + OpenCV破解騰訊防水墻滑動(dòng)驗(yàn)證碼功能
* 驗(yàn)證碼地址:https://007.qq.com/online.html* 使用OpenCv模板匹配* 成功率90%左右* Java + Selenium + OpenCV
產(chǎn)品樣例
來吧!展示!
注意?。?!· 在模擬滑動(dòng)時(shí)不能按照相同速度或者過快的速度滑動(dòng),需要向人滑動(dòng)時(shí)一樣先快后慢,這樣才不容易被識(shí)別。模擬滑動(dòng)代碼↓↓↓
/** * 模擬人工移動(dòng) * @param driver * @param element頁面滑塊 * @param distance需要移動(dòng)距離 */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();}}/** * 根據(jù)距離獲取滑動(dòng)軌跡 * @param distance需要移動(dòng)的距離 * @return */public static List<Integer> getMoveTrack(int distance) {List<Integer> track = new ArrayList<>();// 移動(dòng)軌跡Random random = new Random();int current = 0;// 已經(jīng)移動(dòng)的距離int mid = (int) distance * 4 / 5;// 減速閾值int a = 0;int move = 0;// 每次循環(huán)移動(dòng)的距離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;}
看操作,no bb,直接上代碼
private final String INDEX_URL = 'https://007.qq.com/online.html?ADTAG=index.head';private void seleniumTest() {ChromeDriverManager manager = ChromeDriverManager.getInstance();int status = -1;try {WebDriver driver = manager.getDriver();driver.get(INDEX_URL);driver.manage().window().maximize(); // 設(shè)置瀏覽器窗口最大化Thread.sleep(10000);driver.findElement(By.className('wp-onb-tit')).findElements(By.tagName('a')).get(1).click();Thread.sleep(500);// 點(diǎn)擊出現(xiàn)滑動(dòng)圖waitWebElement(driver, By.id('code'), 500).click();Thread.sleep(100);// 獲取到驗(yàn)證區(qū)域driver.switchTo().frame(waitWebElement(driver, By.id('tcaptcha_iframe'), 500));Thread.sleep(100);// 獲取滑動(dòng)按鈕WebElement moveElemet = waitWebElement(driver, By.id('tcaptcha_drag_button'), 500);Thread.sleep(100);// 獲取帶陰影的背景圖String bgUrl = waitWebElement(driver, By.id('slideBg'), 500).getAttribute('src');Thread.sleep(100);// 獲取帶陰影的小圖String sUrl = waitWebElement(driver, By.id('slideBlock'), 500).getAttribute('src');Thread.sleep(100);// 獲取高度String topStr = waitWebElement(driver, By.id('slideBlock'), 500).getAttribute('style').substring(32, 36);int top = Integer.parseInt(topStr.substring(0, topStr.indexOf('p'))) * 2;Thread.sleep(100);// 計(jì)算移動(dòng)距離int distance = (int) Double.parseDouble(getTencentDistance(bgUrl, sUrl, top));// 滑動(dòng)move(driver, moveElemet, distance);Thread.sleep(5000);} catch (Exception e) {e.printStackTrace();} finally {manager.closeDriver(status);}}/** * 獲取騰訊驗(yàn)證滑動(dòng)距離 * * @return */public static String dllPath = 'C://chrome//opencv_java440.dll';public String getTencentDistance(String bUrl, String sUrl, int top) {System.load(dllPath);File bFile = new File('C:/qq_b.jpg');File sFile = new File('C:/qq_s.jpg');try {FileUtils.copyURLToFile(new URL(bUrl), bFile);FileUtils.copyURLToFile(new URL(sUrl), sFile);BufferedImage bgBI = ImageIO.read(bFile);BufferedImage sBI = ImageIO.read(sFile);// 裁剪bgBI = bgBI.getSubimage(360, top, bgBI.getWidth() - 370, sBI.getHeight());ImageIO.write(bgBI, 'png', bFile);Mat s_mat = Imgcodecs.imread(sFile.getPath());Mat b_mat = Imgcodecs.imread(bFile.getPath());// 轉(zhuǎn)灰度圖像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); // 歸一化平方差匹配法// 歸一化相關(guān)匹配法Core.normalize(g_result, g_result, 0, 1, Core.NORM_MINMAX, -1, new Mat());Point matchLocation = new Point();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, 0, 0, 0));return '' + ((matchLocation.x + s_mat.cols() + 360 - sBI.getWidth() - 46) / 2);} catch (Throwable e) {e.printStackTrace();return null;} finally {bFile.delete();sFile.delete();}}/** * * @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);}}}}}// 延時(shí)加載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() + ')');elseSystem.out.print('.');Thread.sleep(50);}}if (isWait)System.out.println(' outTime!');return null;}
到此這篇關(guān)于使用java + selenium + OpenCV破解騰訊防水墻滑動(dòng)驗(yàn)證碼的文章就介紹到這了,更多相關(guān)java selenium 滑動(dòng)驗(yàn)證碼內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼2. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼3. ASP常用日期格式化函數(shù) FormatDate()4. 網(wǎng)頁中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. python 如何在 Matplotlib 中繪制垂直線9. jsp文件下載功能實(shí)現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
