NativeWebObject.Find Method (Page Objects)

Applies to TestComplete 14.0, last modified on January 23, 2019

Description

Use this method to search for an HTML object on a page using that object’s property value. Note that this method is implemented not only by the Page object, but also by test objects that correspond to frames. The “frame” object’s version is identical to the “page” version.

Also, note that the Page.NativeWebObject.Find method does not search for properties of HTML objects’ children. You can find the target object by its ID using the Find method that is common for all tested objects. The following script code demonstrates how you can do this:

JavaScript, JScript

var obj = page.Find("NativeWebObject.id", id, 500);

Python

obj = page.Find("NativeWebObject.id", id, 500)

VBScript

Set obj = page.Find("NativeWebObject.id", id, 500)

DelphiScript

var obj: OleVariant;
begin
 obj := page.Find('NativeWebObject.id', id, 500);
end;

C++Script, C#Script

var obj = page["Find"]("NativeWebObject.id", id, 500);

The name of the Page.Find method coincides with the name of the Find method that is common for all tested objects (processes and windows). To differ the common Find method and the Page.Find method, the latter was placed in the NativeWebObject namespace, that is, to call it, use the Page(…).NativeWebObject.Find statement. If you write Page(…).Find, TestComplete will call the common Find method.

Declaration

TestObj.NativeWebObject.Find(PropertyNameKeyValueTagName)

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
PropertyName [in]    Required    String    
KeyValue [in]    Required    Variant    
TagName [in]    Optional    String Default value: Empty string   
Result A tested object

Applies To

The method is applied to the following object:

View Mode

To view this method in the Object Browser panel and in other panels and dialogs, activate the Advanced view mode.

Parameters

The method has the following parameters:

PropertyName

Specifies the name of the searched property, for example, contentText. This parameter is case-insensitive.

You can specify both the property names defined in DOM interfaces of web objects (like offsetTop), the names of custom attributes (for example, myAttr in <div id="..." class="..." myAttr="123">) and the names of properties provided by TestComplete (like RowCount for TABLE elements).

KeyValue

Specifies the desired property value. String property values are case-insensitive.

You can use wildcards to indicate variable parts of string values: the asterisk (*) matches a string of any length; the question mark (?) matches any single character.

TagName

Specifies the object’s tag. Use this parameter to limit the scope of the searched objects. This parameter is case-insensitive.

Result Value

If the sought-for object is found, Find returns a TestComplete program object that represents the HTML object sought for in scripts. If several objects match the search criteria, Find posts the “Ambiguous recognition” error to the test log. If the HTML object is not found, Find returns an empty object.

Remarks

The Page.NativeWebObject.Find method allows you to easily obtain the desired element of a Web page by the element’s properties. If an HTML object does not have a Name or ID, TestComplete generates a name for it. Such names look like item(nn) in DOM model and Link_nn or A_nn in the Tag model (nn specifies an object’s index in the collection of its siblings). As you can see, the generated names are not very informative. Also, they depend on the currently selected object model and on the order of objects on the page. For example, if you add a new object to the page, the nn may be changed and you may need to modify the script code to make your scripts work. The Find method lets you find the desired object by key value, rather than the object’s name. This method makes your scripts independent from the current object model and from changes on the page (see below). Another way to solve these problems is to use name mapping.

Example

Suppose you have an HTML page with a button entitled Report, and neither the button’s name, nor the id is specified. The following code illustrates how you can use the Find method to get access to this button in scripts:

JavaScript, JScript

function Without_Find()
{
  // Without Find
  var url = "http://www.sample.com";

  Browsers.Item(btIExplorer).Run(url);
  var page = Sys.Browser().Page(url);
  // Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click();
  // ...
}

function Using_Find()
{
  // Using Find
  var url = "http://www.sample.com";
  
  Browsers.Item(btIExplorer).Run(url);
  var page = Sys.Browser("iexplore").Page(url);
  // Using the Find method
  var myBtn = page.NativeWebObject.Find("value", "Report*", "INPUT");
  // Checks if the button exists
  if (myBtn.Exists)
    myBtn.Click();
  // ...
}

Python

def Without_Find():
  # Without Find
  url = "https://smartbear.com/"
  
  Browsers.Item[btIExplorer].Run(url)
  page = Sys.Browser().Page(url)
  # Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click()
  # ...

def Using_Find():
  # Using Find
  url = "https://smartbear.com/"
  
  Browsers.Item[btIExplorer].Run(url)
  page = Sys.Browser("iexplore").Page(url)
  # Using the Find method
  myBtn = page.NativeWebObject.Find("value", "Report*", "INPUT")
  # Checks if the button exists
  if (myBtn.Exists):
    myBtn.Click()
  # ...

VBScript

Sub Without_Find
  ' Without Find
  url = "http://www.sample.com/"

  Browsers.Item(btIExplorer).Run(url)
  Set page = Sys.Browser.Page(url)
  ' Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click
  ' ...
End Sub

Sub Using_Find
  ' Using Find
  url = "http://www.sample.com/"
  
  Browsers.Item(btIExplorer).Run(url)
  Set page = Sys.Browser.Page(url)
  ' Using the Find method
  Set myBtn = page.NativeWebObject.Find("value", "Report*", "INPUT")
  ' Checks if the button exists
  If myBtn.Exists Then
    myBtn.Click
  End If
  ' ...
End Sub

DelphiScript

procedure Without_Find();
// Without Find
var url, page;
begin
  url := 'http://www.sample.com';
  Browsers.Item(btIExplorer).Run(url);
  page := Sys.Browser.Page(url);
  // Using the button's index in the collection of objects
  page.contentDocument.all.item(12).Click;
  // ...
end;

procedure Using_Find();
// Using Find
var url, page, myBtn;
begin
  url := 'http://www.sample.com';
  Browsers.Item(btIExplorer).Run(url);
  page := Sys.Browser.Page(url);
  // Using the Find method
  myBtn := page.NativeWebObject.Find('value', 'Report*', 'INPUT');
  // Checks if the button exists
  If myBtn.Exists Then
    myBtn.Click;
  // ...
end;

C++Script, C#Script

function Without_Find()
{
  // Without Find
  var url = "http://www.sample.com";
  
  Browsers["Item"](btIExplorer)["Run"](url);
  var page = Sys["Browser"]("iexplore")["Page"](url);
  // Using the button's index in the collection of objects
  page["contentDocument"]["all"]["item"](12)["Click"]();
  // ...
}

function Using_Find()
{
  // Using Find
  var url = "http://www.sample.com";
  
  Browsers["Item"](btIExplorer)["Run"](url);
  var page = Sys["Browser"]("iexplore")["Page"](url);
  // Using the Find method
  var myBtn = page["NativeWebObject"]["Find"]("value", "Report*", "INPUT");
  // Checks if the button exists
  if (myBtn["Exists"])
    myBtn["Click"]();
  // ...
}

See Also

Testing Web Applications
Searching for the Element Containing the Desired Text
EvaluateXPath Method (Page Objects)
ToUrl Method (Page Objects)
Wait Method (Page Objects)
Refresh Method
Tree Model

Highlight search results