Skip to content

Instantly share code, notes, and snippets.

@b1ghawk
Created November 21, 2018 09:39
Show Gist options
  • Save b1ghawk/2d0b5dc72c30666e36e196a80ae4256d to your computer and use it in GitHub Desktop.
Save b1ghawk/2d0b5dc72c30666e36e196a80ae4256d to your computer and use it in GitHub Desktop.
Login into BiliBili (written by HongHong)
/**
*
*/
package 登陆B站;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
* @author school
*
*/
public class Main {
/**
* @param args
* @throws InterruptedException
* @throws IOException
* @throws WebDriverException
*/
public static void main(String[] args) throws InterruptedException, WebDriverException, IOException {
final String USERID = "[email protected]";
final String PASSWORD = "12345678";
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://passport.bilibili.com/login");
webDriver.manage().window().maximize();
while(true)
{
webDriver.findElement(By.xpath("//*[@id=\'login-username\']")).sendKeys(Keys.chord(Keys.CONTROL, "a"),USERID);
webDriver.findElement(By.xpath("//*[@id=\'login-passwd\']")).sendKeys(Keys.chord(Keys.CONTROL, "a"),PASSWORD);
WebElement key = webDriver.findElement(By.xpath("//*[@id=\'gc-box\']/div/div[3]/div[2]"));
Actions action = new Actions(webDriver);
action.moveToElement(key).perform();
Thread.sleep(300);//等待验证码加载出来
BufferedImage image1 = ImageIO.read(((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE));
WebElement kw = webDriver.findElement(By.xpath("//*[@id=\'gc-box\']/div/div[1]/div[2]/div[1]/a[2]/div[1]/div[1]"));
WebElement ka = webDriver.findElement(By.xpath("//*[@id=\'gc-box\']/div/div[1]/div[2]/div[1]/a[2]/div[1]/div[52]"));
image1 = image1.getSubimage(kw.getLocation().x, kw.getLocation().y, Math.abs(ka.getLocation().x-kw.getLocation().x)+ka.getSize().getWidth(),Math.abs(ka.getLocation().y-kw.getLocation().y)+ka.getSize().getHeight());
action.clickAndHold(key).perform();
Thread.sleep(50);//等待验证码加载出来
BufferedImage image2 = ImageIO.read(((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE));
image2 = image2.getSubimage(kw.getLocation().x, kw.getLocation().y, Math.abs(ka.getLocation().x-kw.getLocation().x)+ka.getSize().getWidth(),Math.abs(ka.getLocation().y-kw.getLocation().y)+ka.getSize().getHeight());
action.moveByOffset(Verify.getTX2(image1,image2), 0).perform();
action.release().perform();
Thread.sleep(500);
if(!webDriver.getPageSource().contains("验证失败"))break;
webDriver.navigate().to("https://passport.bilibili.com/login");
}
Runtime.getRuntime().exec("taskkill /F /im " + "chromedriver.exe");
}
// private static boolean ElementExist(WebDriver webDriver, String locator) {
// // TODO 自动生成的方法存根
// try {
// webDriver.findElements(By.name(locator));
// return true;
// } catch (Exception e) {
// return false;
// }
// }
}
/**
*
*/
package 登陆B站;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
* @author school
*
*/
public class Verify {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// // TODO 自动生成的方法存根
//
// // 设置图片地址
// String fristDir = "验证码原图.gif";
// String secondDir = "验证码修改图.gif";
// // 读取图片
// BufferedImage fristGif = getBfImageFromPath(fristDir);
// BufferedImage secondGif = getBfImageFromPath(secondDir);
// // 转换图片为灰度数组
// int[][] fristArray = getImageGray(fristGif);
// int[][] secondArray = getImageGray(secondGif);
// // 获得差异数组
// int[][] reArray = getReArray(fristArray, secondArray);
// // 获得最右边方块最右边的x坐标
// int TX2 = getTwoX2(reArray);
//
// // 打印数组到文件
// arrayToGrayFile(fristArray, "frist.gif");
// arrayToGrayFile(secondArray, "second.gif");
// arrayToGrayFile(reArray, "result.gif");
}
public static int getTX2(BufferedImage fristGif, BufferedImage secondGif) throws IOException {
// 转换图片为灰度数组
int[][] fristArray = getImageGray(fristGif);
int[][] secondArray = getImageGray(secondGif);
// 获得差异数组
int[][] reArray = getReArray(fristArray, secondArray);
// 获得最右边方块最右边的x坐标
int TX2 = getTwoX2(reArray);
return TX2-47;
}
private static int getTwoX2(int[][] reArray) {
// TODO 最右边方块最右边的x坐标
int gifHeight = reArray.length;// 116
int gifWidth = reArray[0].length;// 260
int k=0;
for (int i = gifWidth - 1; i >= 0; i--) {
int sum = 0;
for (int j = 0; j < gifHeight; j++) {
if (reArray[j][i] == 0) {
sum++;
}
}
if (sum >= 20)k++;
if(k==3)return i;
//System.out.printf("%d:\t%d\n",i,sum);
}
return 0;
}
private static int[][] getReArray(int[][] fristArray, int[][] secondArray) {
// TODO 获得差异数组
int gifHeight = fristArray.length;// 116
int gifWidth = fristArray[0].length;// 260
int[][] reArray = new int[gifHeight][gifWidth];
for (int i = 0; i < gifHeight; i++) {
for (int j = 0; j < gifWidth; j++) {
// int a1=fristArray[i][j];
// int a2=secondArray[i][j];
// double c=Math.sqrt(Math.abs(a1*a1-a2*a2));
reArray[i][j] = Integer.MAX_VALUE;
;
if (Math.abs(fristArray[i][j] - secondArray[i][j]) > 3500000) {
reArray[i][j] = 0;
// System.out.printf("(%d,%d)\n", j,i);
// k++;
}
}
}
return reArray;
}
@SuppressWarnings("unused")
private static void arrayToGrayFile(int[][] reArray, String string) throws IOException {
// TODO 数组到文件
BufferedImage grayImage = new BufferedImage(reArray[0].length, reArray.length, 13);
for (int i = 0; i < reArray[0].length; i++) {
for (int j = 0; j < reArray.length; j++) {
grayImage.setRGB(i, j, reArray[j][i]);
}
}
File newFile = new File(string);
ImageIO.write(grayImage, "gif", newFile);
}
@SuppressWarnings("unused")
private static BufferedImage getBfImageFromPath(String keyImagePath) {
// TODO 文件到 BufferedImage
BufferedImage bfImage = null;
try {
bfImage = ImageIO.read(new File(keyImagePath));
} catch (IOException e) {
e.printStackTrace();
}
return bfImage;
}
private static int[][] getImageGray(BufferedImage bufferedImage) throws IOException {
// TODO BufferedImage灰化到数组
// BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth(),
// bufferedImage.getHeight(), bufferedImage.getType());
int result[][] = new int[bufferedImage.getHeight()][bufferedImage.getWidth()];
for (int i = 0; i < bufferedImage.getWidth(); i++) {
for (int j = 0; j < bufferedImage.getHeight(); j++) {
final int color = bufferedImage.getRGB(i, j);
final int r = (color >> 16) & 0xff;
final int g = (color >> 8) & 0xff;
final int b = color & 0xff;
int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
;
// System.out.println(i + " : " + j + " " + gray);
int newPixel = colorToRGB(255, gray, gray, gray);
// grayImage.setRGB(i, j, newPixel);
result[j][i] = newPixel;
}
}
// File newFile = new File(str);
// ImageIO.write(grayImage, "gif", newFile);
return result;
}
private static int colorToRGB(int alpha, int red, int green, int blue) {
// TODO RGB到灰化值
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red;
newPixel = newPixel << 8;
newPixel += green;
newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment