破解验证码的实用技巧:使用 Scala 和 Selenium 实现

在现代网页应用中,图形验证码经常作为登录的一部分,给自动化测试带来了挑战。本文将介绍两种有效的方法来处理验证码:获取验证码图片链接和使用 Selenium 进行截屏和裁剪。

方法一:获取验证码图片地址并下载
我们可以尝试获取验证码的图片链接并下载它。以下是 Scala 语言的实现代码:

scala

import org.openqa.selenium.{WebDriver, WebElement}
import org.openqa.selenium.chrome.ChromeDriver
import java.io.{File, FileOutputStream}
import java.net.URL
import scala.util.Random

object CaptchaDownloader {
def main(args: Array[String]): Unit = {
// 设置 ChromeDriver 路径
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver")
val driver: WebDriver = new ChromeDriver()

try {
// 打开登录页面
driver.get("http://example.com/login")

// 获取验证码图片链接
val captchaElement: WebElement = driver.findElement(By.id("captchaImage"))
val captchaSrc: String = captchaElement.getAttribute("src")
val imgUrl = s"$captchaSrc.png"
val fileName = s"img/login/${Random.nextInt(100000)}.png" // 随机文件名

// 下载验证码图片
val url = new URL(imgUrl)
val connection = url.openConnection()
val inputStream = connection.getInputStream
val outputStream = new FileOutputStream(new File(fileName))

val buffer = new ArraybytesRead = 0
while ( {
bytesRead = inputStream.read(buffer)
bytesRead != -1
}) {
outputStream.write(buffer, 0, bytesRead)
}

outputStream.close()
inputStream.close()
println(s"验证码图片保存成功: $fileName")
} finally {
driver.quit()
}
}
}
方法二:使用 Selenium 截屏并裁剪验证码
如果获取链接不可靠,我们可以通过截屏获取验证码。以下是 Scala 的实现代码:

scala

import org.openqa.selenium.{By, WebDriver, WebElement}
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.OutputType
import org.apache.commons.io.FileUtils
import java.io.File
import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import java.awt.Rectangle

object CaptchaCropper {
def main(args: Array[String]): Unit = {
// 设置 ChromeDriver 路径
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver")
val driver: WebDriver = new ChromeDriver()
更多内容访问ttocr.com或联系1436423940
try {
// 打开登录页面
driver.get("http://example.com/login")

// 截取整个页面
val fullScreenshot: File = driver.getScreenshotAs(OutputType.FILE)

// 定位验证码元素
val captchaElement: WebElement = driver.findElement(By.id("captchaImage"))
val location = captchaElement.getLocation
val size = captchaElement.getSize

// 裁剪验证码
val img = ImageIO.read(fullScreenshot)
val captchaImg = img.getSubimage(location.getX, location.getY, size.getWidth, size.getHeight)
val captchaFileName = s"img/login/${Random.nextInt(100000)}_captcha.png"
ImageIO.write(captchaImg, "png", new File(captchaFileName))

println(s"验证码裁剪并保存成功: $captchaFileName")
} finally {
driver.quit()
}
}
}
输入验证码并登录
最后,我们可以使用 Tesseract OCR 库识别验证码的内容,并将其输入到登录框中。以下是示例代码:

scala

import org.openqa.selenium.{By, WebDriver}
import org.openqa.selenium.chrome.ChromeDriver
import net.sourceforge.tess4j.{Tesseract, TesseractException}
import java.io.File

object CaptchaLogin {
def main(args: Array[String]): Unit = {
// 设置 ChromeDriver 路径
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver")
val driver: WebDriver = new ChromeDriver()

try {
// 打开登录页面
driver.get("http://example.com/login")

// 输入用户名和密码
driver.findElement(By.id("username")).sendKeys("your_username")
driver.findElement(By.id("password")).sendKeys("your_password")

// 识别验证码并输入
val captchaFilePath = "img/login/your_captcha_file.png" // 替换为实际文件路径
val tesseract = new Tesseract()
tesseract.setDatapath("path/to/tessdata")
var captchaText = ""
try {
captchaText = tesseract.doOCR(new File(captchaFilePath))
} catch {
case e: TesseractException => println(s"错误:${e.getMessage}")
}

driver.findElement(By.id("captchaInput")).sendKeys(captchaText.trim)

// 提交登录
driver.findElement(By.id("loginButton")).click()

// 检查登录状态
if (driver.getPageSource.contains("欢迎")) {
println("登录成功!")
} else {
println("登录失败,验证码可能输入错误。")
}
} finally {
driver.quit()
}
}
}

posted @ 2024-11-02 13:33  ttocr、com  阅读(126)  评论(0)    收藏  举报