2424public class CaptchaBreaker {
2525
2626 /**
27- * <b>CapthaManual ()</b>
27+ * <b>CaptchaManual ()</b>
2828 * used to handle captcha in manual
2929 *
3030 * <br><br>
@@ -33,7 +33,7 @@ public class CaptchaBreaker {
3333 *
3434 * @since 1.1
3535 */
36- public static void CapthaManual (int timeout ) {
36+ public static void CaptchaManual (int timeout ) {
3737 try {
3838 Thread .sleep (timeout * 1000 );
3939 } catch (Exception e ) {
@@ -42,7 +42,7 @@ public static void CapthaManual(int timeout) {
4242 }
4343
4444 /**
45- * <b>CapthaAutomation ()</b>
45+ * <b>CaptchaAutomation ()</b>
4646 * used to handle captcha in self action / automatic
4747 *
4848 * <br><br>
@@ -52,54 +52,58 @@ public static void CapthaManual(int timeout) {
5252 *
5353 * @since 1.1
5454 */
55- public static void CapthaAutomation (String xpathCaptchaElement , String idElementInput ) {
55+ public static void CaptchaAutomation (String xpathCaptchaElement , String idElementInput ) {
5656 WebDriver driver = DriverHelper .GetWebDriver ();
5757 WebDriverWait wait = new WebDriverWait (driver , Duration .ofSeconds (FailureHandlingHelper .GetTimeoutWait ()));
58- assert driver != null ;
59- WebElement captchaImage = wait .until (ExpectedConditions .visibilityOfElementLocated (By .xpath (xpathCaptchaElement )));
60- ElementHelper .currentElement = captchaImage ;
58+ try {
59+ assert driver != null ;
60+ WebElement captchaImage = wait .until (ExpectedConditions .visibilityOfElementLocated (By .xpath (xpathCaptchaElement )));
61+ ElementHelper .currentElement = captchaImage ;
6162
62- File tessDataPath = new File ("tessdata" );
63- String tessDataPathString = tessDataPath .getAbsolutePath ();
63+ File tessDataPath = new File ("tessdata" );
64+ String tessDataPathString = tessDataPath .getAbsolutePath ();
6465
65- if (!tessDataPath .exists () && !tessDataPath .isDirectory ()) {
66- new TellMeWhy ("e" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .NOT_FOUND_OCR_TESSDATA );
67- }
66+ if (!tessDataPath .exists () && !tessDataPath .isDirectory ()) {
67+ new TellMeWhy ("e" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .NOT_FOUND_OCR_TESSDATA );
68+ }
6869
69- File screenshot = captchaImage .getScreenshotAs (OutputType .FILE );
70- File folder = new File ("Captcha" );
71- if (!folder .exists ()) {
72- folder .mkdirs ();
73- }
74- File captchaImageFile = new File (folder , "captcha-" + DateTime .getDateTime () + ".png" );
75- BufferedImage cleanedImage = null ;
76- try {
77- FileUtils .copyFile (screenshot , captchaImageFile );
70+ File screenshot = captchaImage .getScreenshotAs (OutputType .FILE );
71+ File folder = new File ("Captcha" );
72+ if (!folder .exists ()) {
73+ folder .mkdirs ();
74+ }
75+ File captchaImageFile = new File (folder , "captcha-" + DateTime .getDateTime () + ".png" );
76+ BufferedImage cleanedImage = null ;
77+ try {
78+ FileUtils .copyFile (screenshot , captchaImageFile );
7879
79- BufferedImage image = ImageIO .read (captchaImageFile );
80- cleanedImage = ImprovImgCaptcha .cleanImage (image );
81- ImageIO .write (cleanedImage , "png" , captchaImageFile );
82- } catch (IOException e ) {
83- new TellMeWhy ("e" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .FAILED_TO_READWRITE_IMG + e .getMessage ());
84- }
80+ BufferedImage image = ImageIO .read (captchaImageFile );
81+ cleanedImage = ImprovImgCaptcha .cleanImage (image );
82+ ImageIO .write (cleanedImage , "png" , captchaImageFile );
83+ } catch (IOException e ) {
84+ new TellMeWhy ("e" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .FAILED_TO_READWRITE_IMG + e .getMessage ());
85+ }
8586
86- Tesseract tesseract = new Tesseract ();
87- tesseract .setDatapath (tessDataPathString );
87+ Tesseract tesseract = new Tesseract ();
88+ tesseract .setDatapath (tessDataPathString );
8889
89- try {
90- String captchaText = tesseract .doOCR (cleanedImage );
91- captchaText = captchaText .replaceAll ("[^a-zA-Z0-9]" , "" );
92- System .out .println ("Captcha text: " + captchaText );
93- if (!captchaText .equalsIgnoreCase ("" )) {
94- TextUtil .InputTextField (idElementInput , captchaText );
95- CapthaManual (8 );
96- } else {
97- TextUtil .InputTextField (idElementInput , TellMeWhy .UNABLE_EXTRACT_CAPTCHA );
98- new TellMeWhy ("w" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .UNABLE_EXTRACT_CAPTCHA );
99- CapthaManual (8 );
90+ try {
91+ String captchaText = tesseract .doOCR (cleanedImage );
92+ captchaText = captchaText .replaceAll ("[^a-zA-Z0-9]" , "" );
93+ System .out .println ("Captcha text: " + captchaText );
94+ if (!captchaText .equalsIgnoreCase ("" )) {
95+ TextUtil .InputTextField (idElementInput , captchaText );
96+ CaptchaManual (8 );
97+ } else {
98+ TextUtil .InputTextField (idElementInput , TellMeWhy .UNABLE_EXTRACT_CAPTCHA );
99+ new TellMeWhy ("w" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .UNABLE_EXTRACT_CAPTCHA );
100+ CaptchaManual (8 );
101+ }
102+ } catch (Exception e ) {
103+ new TellMeWhy ("e" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .ERROR_OCR + e .getMessage ());
100104 }
101105 } catch (Exception e ) {
102- new TellMeWhy ("e" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .ERROR_OCR + e .getMessage ());
106+ new TellMeWhy ("e" , TellMeWhy .getTraceInfo (Thread .currentThread ().getStackTrace ()), TellMeWhy .UNABLE_TO + "Doing Captcha Automation: " + e .getMessage ());
103107 }
104108 }
105109}
0 commit comments