geetest java破解

来源:互联网 发布:windows系统平板 编辑:程序博客网 时间:2024/06/15 11:25
import org.apache.commons.lang3.RandomUtils;import org.openqa.selenium.By;import org.openqa.selenium.Point;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.interactions.Actions;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;import java.awt.image.BufferedImage;import java.util.HashMap;import java.util.List;import java.util.Map;public class ImplicitWaitTest {    public static void main(String args[]) throws Exception {        System.setProperty("webdriver.chrome.driver", "/Applications/Google Chrome.app/Contents/MacOS/chromedriver");        String url2 = "http://localhost:8080/login.jsp";        Map<String, Object> chromeOptions = new HashMap<String, Object>();        chromeOptions.put("binary", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");        DesiredCapabilities capabilities = DesiredCapabilities.chrome();        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);        WebDriver driver = new ChromeDriver(capabilities);        driver.get(url2);        WebElement gt_popup_wrap_e = driver.findElement(By.id("popup-submit"));        Thread.sleep(2000);        gt_popup_wrap_e.click();        (new WebDriverWait(driver, 3))                .until(new ExpectedCondition<Boolean>() {                    @Override                    public Boolean apply(WebDriver d) {                        return d.findElement(By.xpath("//div[@class='gt_slider_knob gt_show']")).isDisplayed();                    }                });        (new WebDriverWait(driver, 3))                .until(new ExpectedCondition<Boolean>() {                    @Override                    public Boolean apply(WebDriver d) {                        return d.findElement(By.xpath("//div[@class='gt_cut_bg gt_show']")).isDisplayed();                    }                });        (new WebDriverWait(driver, 3))                .until(new ExpectedCondition<Boolean>() {                    @Override                    public Boolean apply(WebDriver d) {                        return d.findElement(By.xpath("//div[@class='gt_cut_fullbg gt_show']")).isDisplayed();                    }                });        BufferedImage bufferedImage1 = Utils.getImages(driver, "//div[@class='gt_cut_bg gt_show']/div");        BufferedImage bufferedImage2 = Utils.getImages(driver, "//div[@class='gt_cut_fullbg gt_show']/div");        //找到滑动的圆球        WebElement element = driver.findElement(By.xpath("//div[@class='gt_slider_knob gt_show']"));        //获取缺口位置        int index = Utils.get_diff_location(bufferedImage1, bufferedImage2);        index=index-5;        List<Integer> track_list = Utils.get_diff_location(index);        //生成轨迹        Point point = element.getLocation();        int y = point.getY();        //模拟鼠标的移动        System.out.println("第一步,点击元素");        Actions action = new Actions(driver);        action.clickAndHold(element).perform();        Thread.sleep(150);        for (int i = 0; i < track_list.size(); i++) {            int track = track_list.get(i);            new Actions(driver).moveToElement(element, track + 22, y - RandomUtils.nextInt(442, 445)).perform();            Thread.sleep(RandomUtils.nextInt(10, 50));        }//        new Actions(driver).moveToElement(element, 21, y - 445).perform();//        Thread.sleep(100);//        new Actions(driver).moveToElement(element, 21, y - 445).perform();//        Thread.sleep(100);//        new Actions(driver).moveToElement(element, 21, y - 445).perform();//        new Actions(driver).moveToElement(element, 21, y - 445).perform();//        Thread.sleep(100);//        new Actions(driver).moveToElement(element, 21, y - 445).perform();        Thread.sleep(100);        new Actions(driver).release(element).perform();//        driver.quit();    }}
import org.apache.commons.lang3.RandomUtils;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;/** * Created by yuanyong on 17/4/26. */public class Utils {    public static BufferedImage getBufferedImage(String image) {        File file = new File(image);        BufferedImage bi = null;        try {            bi = ImageIO.read(file);        } catch (Exception e) {            e.printStackTrace();        }        return bi;    }    public static int[] getpixel(BufferedImage bi, int x, int y) throws Exception {        int[] rgb = new int[3];        int pixel = bi.getRGB(x, y); // 下面三行代码将一个数字转换为RGB数字        rgb[0] = (pixel & 0xff0000) >> 16;        rgb[1] = (pixel & 0xff00) >> 8;        rgb[2] = (pixel & 0xff);        return rgb;    }    /**     * 对比RGB值     *     * @param image1     * @param image2     * @param x     * @param y     * @return     * @throws Exception     */    public static boolean is_similar(BufferedImage image1, BufferedImage image2, int x, int y) throws Exception {        int[] pixel1 = getpixel(image1, x, y);        int[] pixel2 = getpixel(image2, x, y);        for (int i = 0; i < 3; i++) {            if (Math.abs(pixel1[i] - pixel2[i]) >= 50) {                return false;            }        }        return true;    }    /**     * 计算缺口的位置     *     * @param image1     * @param image2     * @return     * @throws Exception     */    public static int get_diff_location(BufferedImage image1, BufferedImage image2) throws Exception {        for (int i = 0; i < 260; i++) {            for (int j = 0; j < 116; j++) {                if (is_similar(image1, image2, i, j) == false)                    return i;            }        }        return 0;    }    /**     * 根据缺口的位置模拟x轴移动的轨迹     *     * @param length     * @return     * @throws Exception     */    public static List<Integer> get_diff_location(int length) throws Exception {        int x = RandomUtils.nextInt(1, 3);        List<Integer> list = new ArrayList<>();        while (length - x >= 5) {            list.add(x);            length = length - x;            x = RandomUtils.nextInt(1, 3);        }        for (int i = 0; i < length; i++) {            list.add(1);        }        return list;    }    public static BufferedImage getImages(WebDriver driver, String className) throws IOException {        String imgUrl = "";        List<int[]> positionList = new ArrayList<int[]>();        List<WebElement> elementList = driver.findElements(By.xpath(className));        for (WebElement webElement : elementList) {            String positions = webElement.getCssValue("background-position");            imgUrl = webElement.getCssValue("background-image");            String x = positions.split("px")[0].replace(" ", "");            String y = positions.split("px")[1].replace(" ", "");            positionList.add(new int[]{Integer.valueOf(x), Integer.valueOf(y)});//            System.out.println(webElement.getCssValue("background-position"));        }        imgUrl = imgUrl.replace("url(\"", "").replace("\")", "");        BufferedImage bufferedImage = getGeetestImg(imgUrl, positionList);//        ImageIO.write(bufferedImage, "jpg", new File("/Users/yuanyong/Desktop/3.jpg"));        return bufferedImage;    }    public static BufferedImage getGeetestImg(String strUrl, List<int[]> positionList) throws IOException {        URL url = new URL(strUrl);        HttpURLConnection conn = (HttpURLConnection) url.openConnection();        conn.setConnectTimeout(10 * 1000);        //防止屏蔽程序抓取而返回403错误        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");        //得到输入流        InputStream inputStream = conn.getInputStream();        BufferedImage img = ImageIO.read(inputStream);        List<BufferedImage> list = new ArrayList<BufferedImage>();        for (int i = 0; i < positionList.size(); i++) {//            ImageIO.write(img, "png", new File("/Users/yuanyong/Desktop/Test.png"));            BufferedImage subimage = img.getSubimage(Math.abs(positionList.get(i)[0]), Math.abs(positionList.get(i)[1]), 10, 58);            list.add(subimage);        }        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;    }    public static void main(String args[]) throws Exception {        int index = get_diff_location(getBufferedImage("/Users/yuanyong/Desktop/2.jpg"), getBufferedImage("/Users/yuanyong/Desktop/1.jpg"));        System.out.println(index);        System.out.println(get_diff_location(index));    }}
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>Selenium</groupId>    <artifactId>Selenium</artifactId>    <version>1.0-SNAPSHOT</version>    <profile>        <id>jdk16</id>        <activation>            <activeByDefault>true</activeByDefault>            <jdk>1.6</jdk>        </activation>        <properties>            <maven.compiler.source>1.6</maven.compiler.source>            <maven.compiler.target>1.6</maven.compiler.target>            <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>        </properties>    </profile>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                </configuration>            </plugin>        </plugins>    </build>    <dependencies>    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->    <dependency>        <groupId>org.seleniumhq.selenium</groupId>        <artifactId>selenium-java</artifactId>        <version>3.4.0</version>    </dependency>        <dependency>            <groupId>com.webpimageio</groupId>            <artifactId>webpimageio</artifactId>            <version>0.4.2</version>            <systemPath>${basedir}/src/main/lib/webp-imageio.jar</systemPath>            <scope>system</scope>        </dependency>        <dependency>            <groupId>org.python</groupId>            <artifactId>jython</artifactId>            <version>0.4.2</version>            <systemPath>${basedir}/src/main/lib/jython.jar</systemPath>            <scope>system</scope>        </dependency>        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.12</version>        </dependency>    </dependencies></project>
0 0
原创粉丝点击