OCR code samples (Windows only)

Use OCR to get a string of visible text

This example shows how to use the OCR GetVisibleText method to retrieve all of the text in a browser window.

[Test]
public void TestObjectGetVisibleTextExample()
{
	// Launch the Chrome browser and navigate to the web site.
	var browser = BrowserFactory.Launch(BrowserType.Chrome);
	browser.Navigate("http://www.google.com");
	// Pause to ensure that the browser has navigated to the wanted page.
	Thread.Sleep(4 * 1000);

	// Get the visible text in the entire browser window area.
	string visibleTextInRect = browser.GetVisibleText();
	Assert.IsNotEmpty(visibleTextInRect); 
	// Verify that we retrieved the text visible in the browser window.
    
	// Close the browser
	browser.Close();
}

Back to top

Use OCR to recognize text location

This example shows how to use the OCR GetTextLocations method to retrieve the location of a specific text string.

[Test]
public void TestObjectGetTextLocationsExample()
{
	// Launch the Chrome browser and navigate to the web site.
	var browser = BrowserFactory.Launch(BrowserType.Chrome);
	browser.Navigate("http://www.google.com");
	// Pause to ensure that the browser has navigated to the wanted page.
	Thread.Sleep(4 * 1000);
    
	// Retrieve the location (rectangles) where the string "Google" is found.
	// Search for the string in the entire area of the browser window.
	var textRectangles = browser.GetTextLocations("Google");
	Assert.AreNotEqual(0, textRectangles.Length); 
	// Verify that "Google" text was found in the browser's window, and that there is at least one rectangle with this text.

	browser.Close();
}

Back to top