您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium處理極驗滑動驗證碼
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2016/7/11 14:53:55 ] 推薦標(biāo)簽:功能測試 軟件測試工具

  對比圖片獲取位移方法(deCAPTCHA)是錯的我不放代碼了,下面是其中還原圖片用的方法,目前是其實審查元素后你明白怎么還原這個圖片了,這里是每次讀的10px,58px
public static BufferedImage getGeetestImg(byte[] binary, int[][] imgArray) throws IOException {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(binary));
List<BufferedImage> list = new ArrayList<>();
for (int i=0;i< imgArray.length;i++) {
BufferedImage subimage = img.getSubimage(imgArray[i][0], imgArray[i][1], 10, 58);
list.add(subimage);
//            ImageIO.write(subimage, "jpg", new File("d:\image\imgs"+i+".jpg"));
}
BufferedImage mergeImageUp = null;
BufferedImage mergeImageDown = null;
int mid = list.size()>>>1;
for (int i = 0; i <mid-1 ; i++) {
mergeImageUp =  mergeImage(mergeImageUp==null?list.get(i):mergeImageUp, list.get(i+1), true);
}
for(int i = mid;i<list.size()-1;i++){
mergeImageDown = mergeImage(mergeImageDown==null?list.get(i):mergeImageDown,list.get(i+1), true);
}
img = mergeImage(mergeImageUp, mergeImageDown, false);
return img;
}
public static BufferedImage mergeImage(BufferedImage img1,
BufferedImage img2, boolean isHorizontal) throws IOException {
int w1 = img1.getWidth();
int h1 = img1.getHeight();
int w2 = img2.getWidth();
int h2 = img2.getHeight();
// 從圖片中讀取RGB
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行掃描圖像中各個像素的RGB到數(shù)組中
int[] ImageArrayTwo = new int[w2 * h2];
ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2);
// 生成新圖片
BufferedImage DestImage = null;
if (isHorizontal) { // 水平方向合并
DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 設(shè)置上半部分或左半部分的RGB
DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
} else { // 垂直方向合并
DestImage = new BufferedImage(w1, h1 + h2,
BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 設(shè)置上半部分或左半部分的RGB
DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 設(shè)置下半部分的RGB
}
return DestImage;
}
  2.使用selenium
  后來我想著是我模擬鼠標(biāo)這個動作哪里有問題,我又找到了selenium(2.42.2),他也能操作htmlunit關(guān)鍵他的鼠標(biāo)動作好像封裝比較完全
  但是我嘗試了以后發(fā)現(xiàn)了這個,HtmlUnitMouse這個動作沒有實現(xiàn)
  public void mouseMove(Coordinates where, long xOffset, long yOffset) {
  throw new UnsupportedOperationException("Moving to arbitrary X,Y coordinates not supported.");
  }
  好吧,于是調(diào)用chrome吧
System.setProperty("webdriver.chrome.driver","C:\chromedriver.exe");
Proxy proxy = new Proxy();
//設(shè)置代理服務(wù)器地址
proxy.setHttpProxy("127.0.0.1:8888");
//        DesiredCapabilities capabilities = DesiredCapabilities.htmlUnitWithJs();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.PROXY, proxy);
//        final WebDriver driver = new HtmlUnitDriver(capabilities);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.qixin.com/login");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
checkPage(driver,"return $('.gt_cut_fullbg_slice');");
// 獲取 網(wǎng)頁的 title
System.out.println("1 Page title is: " + driver.getTitle());
// 通過 id 找到 input 的 DOM
String pageSource = driver.getPageSource();
System.out.println(pageSource);
org.openqa.selenium.JavascriptExecutor executor = (org.openqa.selenium.JavascriptExecutor)driver;
boolean equals = executor.executeScript("return document.readyState").equals("complete");
int moveX =99;//移動位置
if (equals) {
WebElement element = driver.findElement(By.className("gt_slider_knob"));//(".gt_slider_knob"));
Point location = element.getLocation();
element.getSize();
Actions action = new Actions(driver);
//             action.clickAndHold().perform();// 鼠標(biāo)在當(dāng)前位置點擊后不釋放
//             action.clickAndHold(element).perform();// 鼠標(biāo)在 onElement 元素的位置點擊后不釋放
//             action.clickAndHold(element).moveByOffset(location.x+99,location.y).release().perform(); //選中source元素->拖放到(xOffset,yOffset)位置->釋放左鍵
action.dragAndDropBy(element, location.x+moveX,location.y).perform();
//            action.dragAndDrop(element,newelement).perform();
pageSource = driver.getPageSource();
}
//更新cookie
Set<org.openqa.selenium.Cookie> cookies = driver.manage().getCookies();
Set<Cookie> cookies2 = new HashSet<>();
for (org.openqa.selenium.Cookie cookie : cookies) {
cookies2.add((Cookie) new Cookie(cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), true));
}
for (Cookie cookie : cookies2) {
org.apache.http.cookie.Cookie httpClient = cookie.toHttpClient();
}
System.out.println(pageSource);
  這樣提交的表單確實是有軌跡的,這里移動位置我先寫了個固定值,可以由上面圖片還原,以及一些開源的圖片識別工具識別出位置。以上應(yīng)該能解決這個滑動驗證碼了

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd