selenium使用ocr识别实现网页动态验证码登录
2017-04-17 16:24
603 查看
算是自己实现的第一个有意义的程序吧,附ocr识别程序下载链接 :http://pan.baidu.com/s/1jHPMktg
目前只在fiefox上测试通过了,IE上面验证码一致都没有识别出来,以后再试试
目前只在fiefox上测试通过了,IE上面验证码一致都没有识别出来,以后再试试
package com.CAPTCHA.tests; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.Point; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; //import org.openqa.selenium.*; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import com.CAPTCHA.tests.*; /** * 登录需要验证码时,自动识别验证码 * @author fang * */ public class TestCAPTCHALogin { public static void main(String[] args) throws IOException, InterruptedException { WebDriver driver = new FirefoxDriver(); //testurl可以替换为你需要登录的网址 driver.get("testurl"); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); WebElement login = driver.findElement(By.linkText("登录")); login.click(); WebElement element = driver.findElement(By.xpath(".//*[@id='validatecodeimg']")); // take screen shot for element screenShotForElement(driver, element, "D:\\Tesseract-OCR\\test.png"); // use Tesseract to get strings Runtime rt = Runtime.getRuntime(); rt.exec("cmd.exe /C tesseract.exe D:\\Tesseract-OCR\\test.png D:\\Tesseract-OCR\\test -1 "); Thread.sleep(1000); // Read text //readTextFile("D:\\Tesseract-OCR\\test.txt"); //String s =readTextFile("D:\\Tesseract-OCR\\test.txt"); System.out.println(readTextFile("D:\\Tesseract-OCR\\test.txt")); String s = readTextFile("D:\\Tesseract-OCR\\test.txt"); //登录 //WebElement searchButton1 = driver.findElement(By.linkText("登录")); //searchButton1.click(); //WebElement searchButton2 = driver.findElement(By.linkText("注册")); //searchButton2.click(); WebElement searchButton3 = driver.findElement(By.id("userName")); searchButton3.sendKeys("username"); WebElement searchButton4 = driver.findElement(By.id("userPass")); searchButton4.sendKeys("123456"); WebElement searchButton5 = driver.findElement(By.xpath(".//*[@id='vadidatecode']")); searchButton5.sendKeys(s); WebElement login1 = driver.findElement(By.id("imageField")); login1.click(); } /** * This method for read TXT file * * @param filePath */ public static String readTextFile(String filePath) { try { String encoding = "GBK"; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding);// 考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { // System.out.println(lineTxt); //return lineTxt; break; } read.close(); return lineTxt; } else { System.out.println("找不到指定的文件"); return null; } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); return null; } } public static void screenShotForElement(WebDriver driver, WebElement element, String path) throws InterruptedException { File scrFile = ((TakesScreenshot) driver) .getScreenshotAs(OutputType.FILE); try { Point p = element.getLocation(); int width = element.getSize().getWidth(); int height = element.getSize().getHeight(); Rectangle rect = new Rectangle(width, height); BufferedImage img = ImageIO.read(scrFile); BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width, rect.height); ImageIO.write(dest, "png", scrFile); Thread.sleep(1000); FileUtils.copyFile(scrFile, new File(path)); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- Python使用selenium实现网页用户名 密码 验证码自动登录功能
- jsp使用servlet实现用户登录 及动态验证码
- 使用Nginx反向代理和内容替换模块实现网页内容动态替换功能
- Linux下使用Google Authenticator配置SSH登录动态验证码
- 怎样使用FreeMarker来实现动态网页静态化
- selenium+ phantomjs实现动态网页爬取
- 【JSP学习笔记】使用Servlet实现网页验证码
- Linux下使用Google Authenticator配置SSH登录动态验证码
- Python使用Selenium和PhantomJS解析动态JS的网页
- 使用selenium+phantomJS实现网页爬取
- 使用selenium的webdriver动态爬取网页时遇到的问题
- Python中使用 Selenium 实现网页截图实例
- PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
- PHP登录验证码的实现与使用方法
- 使用JS实现网页动态换肤
- 使用Selenium2 Python实现网页登陆过程
- Python中使用 Selenium 实现网页截图实例
- 使用jQuery插件轻松实现动态流动的网页布局
- 使用Python+selenium+BeautifulSoup抓取动态网页的关键信息
- Python使用Selenium和PhantomJS解析动态JS的网页