Tesseract OCR to Get Captcha

Hi, I want to make tesseract ocr for read my captcha login. But I’ve an issues while i’m running my katalon script :
DEBUG testcase.GetCaptcha - 12: imageText = image.doOCR(new java.io.File(path))
ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/GetCaptcha FAILED.
Reason:
groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.io.File#.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.lang.String]
[class java.net.URI]
at GetCaptcha.run(GetCaptcha:82)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:138)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:129)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1660540084261.run(TempTestCase1660540084261.groovy:25)

This is my script, please help me :pray:t2:

System.setProperty(‘webdriver.chrome.driver’, DriverFactory.getChromeDriverPath())
WebDriver driver = new ChromeDriver()
driver.get(‘my_url’)
WebElement imageElement = driver.findElement(By.xpath(‘//img[@loading='lazy']’))
File src = imageElement.getScreenshotAs(OutputType.FILE)
String path = System.getProperty(‘base_dir’ + ‘captcha.png’)
FileHandler.copy(src, new File((‘base_dir’ + System.currentTimeMillis()) + ‘.png’))
SLF4JBridgeHandler.removeHandlersForRootLogger()
SLF4JBridgeHandler.install()
ITesseract image = new Tesseract()
String imageText = image.doOCR(new File(path))
System.out.println(imageText)

From this error, the compiler is trying to figure out whether the null you passed is a String or a URI. You can check parsing - Groovy - Ambiguous method overloading for method - Stack Overflow.

I input my directory’s captcha.png in string path :thinking:

The below worked for me.

CustomKeywords.'util.Captcha.getText'()

Custom Keyword Code

package util

import com.kms.katalon.core.annotation.Keyword

import net.sourceforge.tess4j.Tesseract

public class Captcha {

	@Keyword
	public static String getText() {

		'Captcha Screenshot Path'
		String path=System.getProperty("user.dir") + "/Screenshots/CAP.png";
		System.out.println(path);

		Tesseract tesseract = new Tesseract();
		tesseract.setDatapath("D://DataScience//tessdata"); // 'Configure your own path'

		return tesseract.doOCR(new File(path)).trim().replaceAll(" ","")
	}

}