Share this page

# OCR code samples (.NET SDK, Windows only)

In this topic:

## 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.

    // 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();
           Verify.IsNotNullOrEmpty(visibleTextInRect, "Verify Visible Text", "Verify that the browser's window contains some text.");
           // 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.

    // 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");
           Verify.AreNotEqual(0, textRectangles.Length, "Verify Google String Location", "Verify that there is at least one Google string in the browser.");
           // 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

Tips to improve your search results... UFT Developer .NET SDK Reference Version 15.0-15.0.2 UFT Developer SDK Reference All View Local Help Center View Online Help Center back Go back in browser history forward Go forward in browser history Remove search highlights next topic Go to next topic previous topic Go to previous topic Print the current topic Version: