Finding Web Objects Using Common Find Methods

Applies to TestComplete 15.69, last modified on November 13, 2024
Information in this topic applies to web and hybrid mobile applications.

The following topic describes how you can search for web objects from your automated tests using common Find methods.

Searching for Web Objects in Scripts

About Common Find Methods

To find an object on a web page by one or several property values, you can use the following methods:

For example, you can find a link that contains specific text, an image of a certain size, and so on. The difference between these methods is that Find and FindEx starts searching from the current object, whereas FindChild and FindChildEx searches only among child objects and does not check the current object.

The FindAll and FindAllChildren methods, in their turn, allow you to find all the objects that correspond to certain criteria on a web page. For example, you can use them to find all the links that contain specific text. The only difference between these methods is that FindAll starts searching from the current object, whereas FindAllChildren does not check the current object.

We do not recommend using the VisibleOnScreen property in search criteria. It can significantly decrease your test performance because it may take some time for TestComplete to get this property’s value, and TestComplete will have to get the value for every object it checks during the search.

Searching for Web Objects by HTML Attributes

TestComplete can access native attributes of web elements and treats these attributes as object properties. This means that you can use Find methods to search for web objects by their HTML attributes. For example, you can find an image with a particular ALT attribute, an element having a certain NG-MODEL attribute of the AngularJS framework, and so on.

There can be issues when the property and attribute values returned by the browser differ from those defined in the web page's source code. For more information on such issues, see Important Notes.

Specifying the Search Scope

You can specify the search scope using the methods'Depth parameter. This parameter specifies the level of objects where the method will search for the desired object. The default value is 1, that is the method will search only in the specified object and its children (or only within children if you are using FindChild or FindAllChildren). If you want to search on the whole page, increase the value of the Depth parameter to extend the search scope. Otherwise, the desired object may not be found.

To search on the whole web page, start searching from the object that corresponds to the tested web page and specify the maximum depth to search in the entire object hierarchy.

When searching using FindAll and FindAllChildren, you can limit the search depth by choosing an appropriate object to call any common Find method. For example, to find a link on a page, you can call the Find method of the container object that contains the desired link instead of searching among all page objects.

Depth-First vs Breadth-First Search

You can use the project property Object search strategy to choose the search order. You can change this option at run time via the Options.Run.ObjectSearchStrategy property. If the search is slow, try changing this option to the opposite of what it is set to. Note that the results may vary for different applications or even for different parts of the same application.

Search Result

The Find, FindEx, FindChild and FindChildEx methods return the found object if the search succeeds. If the search fails, the methods return an empty "stub" object. To find out whether the search was successful, check the Exists property of the returned object.

The FindAll and FindAllChildren methods return an array of all found objects that correspond to the specified criteria. To determine the number of found objects, check the length of the returned array (for example, if no objects are found, the array length is 0).

Remarks

If your page contains dynamic objects, set the method's RefreshTree parameter to True. If no object is found, TestComplete will refresh the object tree and perform the search again.

For more information on how to search for objects from scripts, see the Searching for an Object topic.

Examples

FindChild Example

The following example uses the FindChild method to find a web page's link that contains specific text in Internet Explorer or Android webview, and then clicks it.

Web (Classic)

JavaScript, JScript

function findChild()
{

  var url = "http://smartbear.com/";

  // Launch Internet Explorer if it is not running
  if (! Sys.WaitBrowser("iexplore", 10000).Exists)
    Browsers.Item(btIExplorer).Run(url);
  else
    Browsers.Item(btIExplorer).Navigate(url);

  var page = Sys.Browser("*").Page(url);

  // Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "Products"];

  // Find an object that corresponds to the specified criteria
  link = page.FindChild(props, values, 10);
  link.Click();

}

Python

def findChild():
  url = "http://smartbear.com/";

  # Launch Internet Explorer if it is not running
  if not Sys.WaitBrowser("iexplore", 10000).Exists:
    Browsers.Item[btIExplorer].Run(url);
  else:
    Browsers.Item[btIExplorer].Navigate(url);

  page = Sys.Browser("*").Page(url);

  # Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "Products"];

  # Find an object that corresponds to the specified criteria
  link = page.FindChild(props, values, 10);
  link.Click();

VBScript

Sub findChild
  Dim url, page, props, values, link

  url = "http://smartbear.com/"

  ' Launch Internet Explorer if it is not running
  If Not Sys.WaitBrowser("iexplore", 10000).Exists Then
    Browsers.Item(btIExplorer).Run url
  Else
    Browsers.Item(btIExplorer).Navigate url
  End If

  Set page = Sys.Browser("*").Page(url)

  ' Specify the sought-for properties and values
  props = Array("tagName", "contentText")
  values = Array("a", "Products")

  ' Find an object that corresponds to the specified criteria
  Set link = page.FindChild(props, values, 10)
  link.Click

End Sub

DelphiScript

procedure findChild;
var url, page, props, values, link;
begin

  url := 'http://smartbear.com/';

  // Launch Internet Explorer if it is not running
  if not Sys.WaitBrowser('iexplore', 10000).Exists then
    Browsers.Item[btIExplorer].Run(url)
  else
    Browsers.Item[btIExplorer].Navigate(url);

  page := Sys.Browser('*').Page(url);

  // Specify the sought-for properties and values
  props := ['tagName', 'contentText'];
  values := ['a', 'Products'];

  // Find an object that corresponds to the specified criteria
  link := page.FindChild(props, values, 10);
  link.Click;

end;

C++Script, C#Script

function findChild()
{

  var url = "http://smartbear.com/";

  // Launch Internet Explorer if it is not running
  if (! Sys["WaitBrowser"]("iexplore", 10000).Exists)
    Browsers["Item"](btIExplorer)["Run"](url);
  else
    Browsers["Item"](btIExplorer)["Navigate"](url);

  var page = Sys["Browser"]("*")["Page"](url);

  // Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "Products"];

  // Find an object that corresponds to the specified criteria
  link = page["FindChild"](props, values, 10);
  link["Click"]();

}

Mobile (Legacy)

JavaScript, JScript

function findChild1()
{

  var url = "http://smartbear.com/";

  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  var page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  // On iOS devices
  // var app = Mobile.Device().Process("SampleWebViewApp");
  // var page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  // Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "Products"];

  // Find an object that corresponds to the specified criteria
  link = page.FindChild(props, values, 10);
  link.Click();
}

Python

def findChild1():
  url = "http://smartbear.com/";

  # Select the mobile device 
  Mobile.SetCurrent("MyDevice");

  # Obtain the WebView object and navigate to a web page 
  # On Android devices 
  app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  # On iOS devices 
  # var app = Mobile.Device().Process("SampleWebViewApp");
  # var page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  # Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "Products"];

  # Find an object that corresponds to the specified criteria
  link = page.FindChild(props, values, 10);
  link.Click();

VBScript

Sub findChild1
  Dim app, url, page, props, values, link

  url = "http://smartbear.com/"
  
  ' Select the mobile device
  Mobile.SetCurrent("MyDevice")

  ' Obtain the WebView object and navigate to a web page
  ' On Android devices
  Set app = Mobile.Device.Process("smartbear.tctests.webbrowserapp")
  Set page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl (url)
  ' On iOS devices
  ' Set app = Mobile.Device.Process("SampleWebViewApp")
  ' Set page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl (url)

  ' Specify the sought-for properties and values
  props = Array("tagName", "contentText")
  values = Array("a", "Products")

  ' Find an object that corresponds to the specified criteria
  Set link = page.FindChild(props, values, 10)
  link.Click
End Sub

DelphiScript

procedure findChild1;
var url, app, page, props, values, link;
begin

  url := 'http://smartbear.com/';

  // Select the mobile device
  Mobile.SetCurrent('MyDevice');

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  app := Mobile.Device.Process('smartbear.tctests.webbrowserapp');
  page := app.RootLayout('').Layout('layoutTop').WebView('webview').Page('file:///android_asset/page1.html').ToUrl(url);
  // On iOS devices
  // app := Mobile.Device.Process('SampleWebViewApp');
  // page := app.Window(0).WebView(0).Page('http://www.example.com').ToUrl(url);

  // Specify the sought-for properties and values
  props := ['tagName', 'contentText'];
  values := ['a', 'Products'];

  // Find an object that corresponds to the specified criteria
  link := page.FindChild(props, values, 10);
  link.Click;
end;

C++Script, C#Script

function findChild1()
{
  var url = "http://smartbear.com/";

  // Select the mobile device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile["Device"].Process("smartbear.tctests.webbrowserapp");
  var page = app["RootLayout"]("")["Layout"]("layoutTop")["WebView"]("webview")["Page"]("file:///android_asset/page1.html")["ToUrl"](url)
  // On iOS devices
  // var app = Mobile["Device"].Process("SampleWebViewApp");
  // var page = app["Window"](0)["WebView"](0)["Page"]("http://www.example.com")["ToUrl"](url)

  // Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "Products"];

  // Find an object that corresponds to the specified criteria
  link = page["FindChild"](props, values, 10);
  link["Click"]();
}

FindAllChildren Example

The example below uses the FindAllChildren method. This script code demonstrates how you can search for multiple objects that match certain criteria on a web page in Internet Explorer or Android WebView. It finds and reports all the links located on the SmartBear Software home page that contain the word TestComplete:

Note: The only difference between scripts for web and hybrid mobile applications is in the way you obtain the Page object.

Web (Classic)

JavaScript, JScript

function findAllChildren()
{
  var url = "http://smartbear.com/";

  // Launch Internet Explorer if it is not running
  if (! Sys.WaitBrowser("iexplore", 10000).Exists)
    Browsers.Item(btIExplorer).Run(url);
  else
    Browsers.Item(btIExplorer).Navigate(url);

  var page = Sys.Browser("*").Page(url);

  // Specify the sought-for properties and values
  var props = ["tagName", "contentText"];
  var values = ["a", "*TestComplete*"];

  // Find all objects that correspond to the specified criteria
  var children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  var cnt = links.length;
  if (cnt > 0)
  {
    Log.Message(cnt + " object(s) were found:");
    for (var i = 0; i < cnt; i++)
      Log.Message(links[i].contentText, links[i].FullName);
  }
  else
  {
    Log.Message("No objects corresponding to the specified criteria were found.");
  }
}

Python

def findAllChildren():
  url = "http://smartbear.com/";

  # Launch Internet Explorer if it is not running 
  if not Sys.WaitBrowser("iexplore", 10000).Exists:
    Browsers.Item(btIExplorer).Run(url);
  else:
    Browsers.Item(btIExplorer).Navigate(url);

  page = Sys.Browser("*").Page(url);

  # Specify the sought-for properties and values 
  props = ["tagName", "contentText"];
  values = ["a", "*TestComplete*"]; 

  # Find all objects that correspond to the specified criteria 
  children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  cnt = links.length;
  if (cnt > 0):
    Log.Message(cnt + " object(s) were found:");
    for i in range (0, cnt-1):
      Log.Message(links[i].contentText, links[i].FullName); 
  else: 
    Log.Message("No objects corresponding to the specified criteria were found.");

VBScript

Sub findAllChildren
  Dim url, page, props, values, links, link

  url = "http://smartbear.com/"

  ' Launch Internet Explorer if it is not running
  If Not Sys.WaitBrowser("iexplore", 10000).Exists Then
    Browsers.Item(btIExplorer).Run url
  Else
    Browsers.Item(btIExplorer).Navigate url
  End If

  Set page = Sys.Browser("*").Page(url)

  ' Specify the sought-for properties and values
  props = Array("tagName", "contentText")
  values = Array("a", "*TestComplete*")

  ' Find all objects that correspond to the specified criteria
  links = page.FindAllChildren(props, values, 10)

  If UBound(links) >= 0 Then
    Log.Message (UBound(links) + 1) & " object(s) were found:"
    For Each link In links
      Log.Message link.contentText, link.FullName
    Next
  Else
    Log.Message "No objects corresponding to the specified criteria were found."
  End If
End Sub

DelphiScript

procedure findAllChildren;
var url, page, props, values, links, i, cnt;
begin

  url := 'http://smartbear.com/';

  // Launch Internet Explorer if it is not running
  if not Sys.WaitBrowser('iexplore', 10000).Exists then
    Browsers.Item[btIExplorer].Run(url)
  else
    Browsers.Item[btIExplorer].Navigate(url);

  page := Sys.Browser('*').Page(url);

  // Specify the sought-for properties and values
  props := ['tagName', 'contentText'];
  values := ['a', '*TestComplete*'];

  // Find all objects that correspond to the specified criteria
  links := page.FindAllChildren(props, values, 10);

  cnt := VarArrayHighBound(links, 1) + 1;
  if cnt > 0 then
  begin
    Log.Message(VarToStr(cnt) + ' object(s) were found:');
    for i := 0 to cnt - 1 do
      Log.Message(links[i].contentText, links[i].FullName);
  end
  else
    Log.Message('No objects corresponding to the specified criteria were found.');
end;

C++Script, C#Script

function findAllChildren()
{
  var url = "http://smartbear.com/";

  // Launch Internet Explorer if it is not running
  if (! Sys["WaitBrowser"]("iexplore", 10000).Exists)
    Browsers["Item"](btIExplorer)["Run"](url);
  else
    Browsers["Item"](btIExplorer)["Navigate"](url);

  var page = Sys["Browser"]("*")["Page"](url);

  // Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "*TestComplete*"];

  // Find all objects that correspond to the specified criteria
  var children = page["FindAllChildren"](props, values, 10);
  links = children["toArray"]();

  cnt = links["length"];
  if (cnt > 0)
  {
    Log["Message"](cnt + " object(s) were found:");
    for (i = 0; i < cnt; i++)
      Log["Message"](links[i]["contentText"], links[i]["FullName"]);
  }
  else
  {
    Log["Message"]("No objects corresponding to the specified criteria were found.");
  }
}

Mobile (Legacy)

JavaScript, JScript

function findAllChildren1()
{
  var url = "http://smartbear.com/";

  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  var page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  // On iOS devices
  // var app = Mobile.Device().Process("SampleWebViewApp");
  // var page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  // Specify the sought-for properties and values
  var props = ["tagName", "contentText"];
  var values = ["a", "*TestComplete*"];

  // Find all objects that correspond to the specified criteria
  var children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  var cnt = links.length;
  if (cnt > 0)
  {
    Log.Message(cnt + " object(s) were found:");
    for (var i = 0; i < cnt; i++)
      Log.Message(links[i].contentText, links[i].FullName);
  }
  else
  {
    Log.Message("No objects corresponding to the specified criteria were found.");
  }
}

Python

def findAllChildren1():
  url = "http://smartbear.com/";

  # Select the mobile device 
  Mobile.SetCurrent("MyDevice");

  # Obtain the WebView object and navigate to a web page 
  # On Android devices 
  app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  # On iOS devices 
  # app = Mobile.Device().Process("SampleWebViewApp");
  # page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  # Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "*TestComplete*"]; 

  # Find all objects that correspond to the specified criteria
  children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  cnt = links.length;
  if (cnt > 0):
    Log.Message(cnt + " object(s) were found:");
    for i in range (0, cnt-1):
      Log.Message(links[i].contentText, links[i].FullName); 
  else:
    Log.Message("No objects corresponding to the specified criteria were found.");

VBScript

Sub findAllChildren1
  Dim url, page, props, values, links, link

  url = "http://smartbear.com/"

  ' Select the mobile device
  Mobile.SetCurrent("MyDevice")

  ' Obtain the WebView object and navigate to a web page
  ' On Android devices
  Set app = Mobile.Device.Process("smartbear.tctests.webbrowserapp")
  Set page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl (url)
  ' On iOS devices
  ' Set app = Mobile.Device.Process("SampleWebViewApp")
  ' Set page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl (url)

  ' Specify the sought-for properties and values
  props = Array("tagName", "contentText")
  values = Array("a", "*TestComplete*")

  ' Find all objects that correspond to the specified criteria
  links = page.FindAllChildren(props, values, 10)

  If UBound(links) >= 0 Then
    Log.Message (UBound(links) + 1) & " object(s) were found:"
    For Each link In links
      Log.Message link.contentText, link.FullName
    Next
  Else
    Log.Message "No objects corresponding to the specified criteria were found."
  End If
End Sub

DelphiScript

procedure findAllChildren1;
var 
  url, app, page, props, values, links, i, cnt;
begin

  url := 'http://smartbear.com/';

  // Select the mobile device
  Mobile.SetCurrent('MyDevice');

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  app := Mobile.Device.Process('smartbear.tctests.webbrowserapp');
  page := app.RootLayout('').Layout('layoutTop').WebView('webview').Page('file:///android_asset/page1.html').ToUrl(url);
  // On iOS devices
  // app := Mobile.Device.Process('SampleWebViewApp');
  // page := app.Window(0).WebView(0).Page('http://www.example.com').ToUrl(url);

  // Specify the sought-for properties and values
  props := ['tagName', 'contentText'];
  values := ['a', '*TestComplete*'];

  // Find all objects that correspond to the specified criteria
  links := page.FindAllChildren(props, values, 10);

  cnt := VarArrayHighBound(links, 1) + 1;
  if cnt > 0 then
  begin
    Log.Message(VarToStr(cnt) + ' object(s) were found:');
    for i := 0 to cnt - 1 do
      Log.Message(links[i].contentText, links[i].FullName);
  end
  else
    Log.Message('No objects corresponding to the specified criteria were found.');
end;

C++Script, C#Script

function findAllChildren1()
{
  var url = "http://smartbear.com/";

  // Select the mobile device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile["Device"].Process("smartbear.tctests.webbrowserapp");
  var page = app["RootLayout"]("")["Layout"]("layoutTop")["WebView"]("webview")["Page"]("file:///android_asset/page1.html")["ToUrl"](url)
  // On iOS devices
  // var app = Mobile["Device"].Process("SampleWebViewApp");
  // var page = app["Window"](0)["WebView"](0)["Page"]("http://www.example.com")["ToUrl"](url)

  // Specify the sought-for properties and values
  props = ["tagName", "contentText"];
  values = ["a", "*TestComplete*"];

  // Find all objects that correspond to the specified criteria
  var children = page["FindAllChildren"](props, values, 10);
  links = children["toArray"]();

  cnt = links["length"];
  if (cnt > 0)
  {
    Log["Message"](cnt + " object(s) were found:");
    for (i = 0; i < cnt; i++)
      Log["Message"](links[i]["contentText"], links[i]["FullName"]);
  }
  else
  {
    Log["Message"]("No objects corresponding to the specified criteria were found.");
  }
}

The following example uses the FindAllChildren method to find all links that open in a new window, that is, all links having the target="_blank" rel="noopener noreferrer" HTML attribute.

Web (Classic)

JavaScript, JScript

function findAllChildren2()
{
  var url = "http://smartbear.com/";

  // Launch Internet Explorer if it is not running
  if (! Sys.WaitBrowser("iexplore", 10000).Exists)
    Browsers.Item(btIExplorer).Run(url);
  else
    Browsers.Item(btIExplorer).Navigate(url);

  var page = Sys.Browser("*").Page(url);

  // Specify the sought-for properties and values
  var props = ["tagName", "target"];
  var values = ["a", "_blank"];

  // Find all objects that correspond to the specified criteria
  var children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  var cnt = links.length;
  if (cnt > 0)
  {
    Log.Message(cnt + " object(s) were found:");
    for (var i = 0; i < cnt; i++)
      Log.Message(links[i].contentText, links[i].FullName);
  }
  else
  {
    Log.Message("No objects corresponding to the specified criteria were found.");
  }
}

Python

def findAllChildren2():
  url = "http://smartbear.com/";

  # Launch Internet Explorer if it is not running 
  if not Sys.WaitBrowser("iexplore", 10000).Exists:
    Browsers.Item(btIExplorer).Run(url);
  else:
    Browsers.Item(btIExplorer).Navigate(url);

  page = Sys.Browser("*").Page(url);

  # Specify the sought-for properties and values 
  props = ["tagName", "target"];
  values = ["a", "_blank"]; 

  # Find all objects that correspond to the specified criteria 
  children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  cnt = links.length;
  if (cnt > 0):
    Log.Message(cnt + " object(s) were found:");
    for i in range (0, cnt-1):
      Log.Message(links[i].contentText, links[i].FullName); 
  else: 
    Log.Message("No objects corresponding to the specified criteria were found.");

VBScript

Sub findAllChildren2
  Dim url, page, props, values, links, link

  url = "http://smartbear.com/"

  ' Launch Internet Explorer if it is not running
  If Not Sys.WaitBrowser("iexplore", 10000).Exists Then
    Browsers.Item(btIExplorer).Run url
  Else
    Browsers.Item(btIExplorer).Navigate url
  End If

  Set page = Sys.Browser("*").Page(url)

  ' Specify the sought-for properties and values
  props = Array("tagName", "target")
  values = Array("a", "_blank")

  ' Find all objects that correspond to the specified criteria
  links = page.FindAllChildren(props, values, 10)

  If UBound(links) >= 0 Then
    Log.Message (UBound(links) + 1) & " object(s) were found:"
    For Each link In links
      Log.Message link.contentText, link.FullName
    Next
  Else
    Log.Message "No objects corresponding to the specified criteria were found."
  End If
End Sub

DelphiScript

procedure findAllChildren2;
var url, page, props, values, links, i, cnt;
begin

  url := 'http://smartbear.com/';

  // Launch Internet Explorer if it is not running
  if not Sys.WaitBrowser('iexplore', 10000).Exists then
    Browsers.Item[btIExplorer].Run(url)
  else
    Browsers.Item[btIExplorer].Navigate(url);

  page := Sys.Browser('*').Page(url);

  // Specify the sought-for properties and values
  props := ['tagName', 'target'];
  values := ['a', '_blank'];

  // Find all objects that correspond to the specified criteria
  links := page.FindAllChildren(props, values, 10);

  cnt := VarArrayHighBound(links, 1) + 1;
  if cnt > 0 then
  begin
    Log.Message(VarToStr(cnt) + ' object(s) were found:');
    for i := 0 to cnt - 1 do
      Log.Message(links[i].contentText, links[i].FullName);
  end
  else
    Log.Message('No objects corresponding to the specified criteria were found.');
end;

C++Script, C#Script

function findAllChildren2()
{
  var url = "http://smartbear.com/";

  // Launch Internet Explorer if it is not running
  if (! Sys["WaitBrowser"]("iexplore", 10000).Exists)
    Browsers["Item"](btIExplorer)["Run"](url);
  else
    Browsers["Item"](btIExplorer)["Navigate"](url);

  var page = Sys["Browser"]("*")["Page"](url);

  // Specify the sought-for properties and values
  props = ["tagName", "target"];
  values = ["a", "_blank"];

  // Find all objects that correspond to the specified criteria
  var children = page["FindAllChildren"](props, values, 10);
  links = children["toArray"]();

  cnt = links["length"];
  if (cnt > 0)
  {
    Log["Message"](cnt + " object(s) were found:");
    for (i = 0; i < cnt; i++)
      Log["Message"](links[i]["contentText"], links[i]["FullName"]);
  }
  else
  {
    Log["Message"]("No objects corresponding to the specified criteria were found.");
  }
}

Mobile (Legacy)

JavaScript, JScript

function findAllChildren3()
{
  var url = "http://smartbear.com/";

  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  var page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  // On iOS devices
  // var app = Mobile.Device().Process("SampleWebViewApp");
  // var page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  // Specify the sought-for properties and values
  var props = ["tagName", "target"];
  var values = ["a", "_blank"];

  // Find all objects that correspond to the specified criteria
  var children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  var cnt = links.length;
  if (cnt > 0)
  {
    Log.Message(cnt + " object(s) were found:");
    for (var i = 0; i < cnt; i++)
      Log.Message(links[i].contentText, links[i].FullName);
  }
  else
  {
    Log.Message("No objects corresponding to the specified criteria were found.");
  }
}

Python

def findAllChildren3():
  url = "http://smartbear.com/";

  # Select the mobile device 
  Mobile.SetCurrent("MyDevice");

  # Obtain the WebView object and navigate to a web page 
  # On Android devices 
  app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  # On iOS devices 
  # app = Mobile.Device().Process("SampleWebViewApp");
  # page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  # Specify the sought-for properties and values
  props = ["tagName", "target"];
  values = ["a", "_blank"]; 

  # Find all objects that correspond to the specified criteria
  children = page.FindAllChildren(props, values, 10);
  links = children.toArray();

  cnt = links.length;
  if (cnt > 0):
    Log.Message(cnt + " object(s) were found:");
    for i in range (0, cnt-1):
      Log.Message(links[i].contentText, links[i].FullName); 
  else:
    Log.Message("No objects corresponding to the specified criteria were found.");

VBScript

Sub findAllChildren3
  Dim url, page, props, values, links, link

  url = "http://smartbear.com/"

  ' Select the mobile device
  Mobile.SetCurrent("MyDevice")

  ' Obtain the WebView object and navigate to a web page
  ' On Android devices
  Set app = Mobile.Device.Process("smartbear.tctests.webbrowserapp")
  Set page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl (url)
  ' On iOS devices
  ' Set app = Mobile.Device.Process("SampleWebViewApp")
  ' Set page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl (url)

  ' Specify the sought-for properties and values
  props = Array("tagName", "target")
  values = Array("a", "_blank")

  ' Find all objects that correspond to the specified criteria
  links = page.FindAllChildren(props, values, 10)

  If UBound(links) >= 0 Then
    Log.Message (UBound(links) + 1) & " object(s) were found:"
    For Each link In links
      Log.Message link.contentText, link.FullName
    Next
  Else
    Log.Message "No objects corresponding to the specified criteria were found."
  End If
End Sub

DelphiScript

procedure findAllChildren3;
var 
  url, app, page, props, values, links, i, cnt;
begin

  url := 'http://smartbear.com/';

  // Select the mobile device
  Mobile.SetCurrent('MyDevice');

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  app := Mobile.Device.Process('smartbear.tctests.webbrowserapp');
  page := app.RootLayout('').Layout('layoutTop').WebView('webview').Page('file:///android_asset/page1.html').ToUrl(url);
  // On iOS devices
  // app := Mobile.Device.Process('SampleWebViewApp');
  // page := app.Window(0).WebView(0).Page('http://www.example.com').ToUrl(url);

  // Specify the sought-for properties and values
  props := ['tagName', 'target'];
  values := ['a', '_blank'];

  // Find all objects that correspond to the specified criteria
  links := page.FindAllChildren(props, values, 10);

  cnt := VarArrayHighBound(links, 1) + 1;
  if cnt > 0 then
  begin
    Log.Message(VarToStr(cnt) + ' object(s) were found:');
    for i := 0 to cnt - 1 do
      Log.Message(links[i].contentText, links[i].FullName);
  end
  else
    Log.Message('No objects corresponding to the specified criteria were found.');
end;

C++Script, C#Script

function findAllChildren3()
{
  var url = "http://smartbear.com/";

  // Select the mobile device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile["Device"].Process("smartbear.tctests.webbrowserapp");
  var page = app["RootLayout"]("")["Layout"]("layoutTop")["WebView"]("webview")["Page"]("file:///android_asset/page1.html")["ToUrl"](url)
  // On iOS devices
  // var app = Mobile["Device"].Process("SampleWebViewApp");
  // var page = app["Window"](0)["WebView"](0)["Page"]("http://www.example.com")["ToUrl"](url)

  // Specify the sought-for properties and values
  props = ["tagName", "target"];
  values = ["a", "_blank"];

  // Find all objects that correspond to the specified criteria
  var children = page["FindAllChildren"](props, values, 10);
  links = children["toArray"]();

  cnt = links["length"];
  if (cnt > 0)
  {
    Log["Message"](cnt + " object(s) were found:");
    for (i = 0; i < cnt; i++)
      Log["Message"](links[i]["contentText"], links[i]["FullName"]);
  }
  else
  {
    Log["Message"]("No objects corresponding to the specified criteria were found.");
  }
}

Searching for Web Objects in Keyword Tests

To simplify searching for objects from keyword tests, TestComplete includes the Find Object operation that allows you to search for an object, and if this object is found, call its method or property. Below is a sample keyword test that searches for a link on the tested web page and clicks it:

If needed, you can also call one of available search methods by using the Call Object Method or Run Code Snippet operation. Also, you can write a script routine that will perform a search and call it from your keyword test by using the Run Script Routine operation.

For details on how to search for objects from keyword tests, see the Searching for an Object topic.

See Also

Searching for an Object
Find Web Objects
Finding Web Objects Using Specific Find Method
How To
Access Native Web Attributes and Methods
Classic Web Testing

Highlight search results