OCR code samples (Windows only)

Recognize text using OCR

The following example shows how to use OCR to get text on a Web page using the GetVisibleText method.

@Test
public void testGetVisibleTextExample() throws Exception {				
 // Launch the Chrome browser and navigate to the web site.
 Browser browser = BrowserFactory.launch(BrowserType.CHROME);				
 browser.navigate("http://www.google.com");
				
 // Pause to ensure the browser has navigated to the wanted page.
 .sleep(4 * 1000);
				
 // Get the visible text in the browser's entire window area
 String textInBrowser = browser.getVisibleText();
				
 // Verify we have some text
 assertNotNull(textInBrowser); 
 // Close the browser				
 browser.close();				
}			

Back to top

Get text location using OCR

The following example shows how to use OCR to get a text location on a Web page using the GetTextLocations method.

@Test
public void testObjectGetTextLocationsExample() throws Exception {				
 // Launch the Chrome browser and navigate to the web site.
 Browser browser = BrowserFactory.launch(BrowserType.CHROME);				
 browser.navigate("http://www.google.com");
				
 // Pause to ensure the browser has navigated to the wanted page.
 Thread.sleep(4 * 1000);
				
 // Get the rectangles where the text "google" exists in the browser's entire window area.
 Rectangle[] rectangles = browser.getTextLocations("Google"); 

 // Verify that the "Google" text was found in the browser's window.
 // Also verify that there is at least one rectangle with this text.				
 assertNotEquals(0, rectangles.length);
				
 browser.close();
				
}			

Back to top

See also: