您的位置:软件测试 > 开源软件测试 > 开源功能测试工具 > Selenium
Selenium处理极验滑动验证码
作者:网络转载 发布时间:[ 2016/7/11 14:53:55 ] 推荐标签:功能测试 软件测试工具

  对比图片获取位移方法(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到数组中
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); // 设置上半部分或左半部分的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); // 设置上半部分或左半部分的RGB
DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 设置下半部分的RGB
}
return DestImage;
}
  2.使用selenium
  后来我想着是我模拟鼠标这个动作哪里有问题,我又找到了selenium(2.42.2),他也能操作htmlunit关键他的鼠标动作好像封装比较完全
  但是我尝试了以后发现了这个,HtmlUnitMouse这个动作没有实现
  public void mouseMove(Coordinates where, long xOffset, long yOffset) {
  throw new UnsupportedOperationException("Moving to arbitrary X,Y coordinates not supported.");
  }
  好吧,于是调用chrome吧
System.setProperty("webdriver.chrome.driver","C:\chromedriver.exe");
Proxy proxy = new Proxy();
//设置代理服务器地址
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');");
// 获取 网页的 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();// 鼠标在当前位置点击后不释放
//             action.clickAndHold(element).perform();// 鼠标在 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);
  这样提交的表单确实是有轨迹的,这里移动位置我先写了个固定值,可以由上面图片还原,以及一些开源的图片识别工具识别出位置。以上应该能解决这个滑动验证码了

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd