FindElements Method (Page Objects)

Applies to TestComplete 14.91, last modified on July 20, 2021

Description

Use the FindElements method to find all web elements that match a search condition. You can specify the condition either by using the XPath syntax (see www.w3schools.com/xml/xpath_syntax) or the CSS selector syntax (see www.w3.org/TR/selectors).

Requirements

  • TestComplete 14.5.

    Note: If you have another version of TestComplete, please check the requirements for your version.

  • An active license for the TestComplete Web Module.

  • The Web Testing plugin must be enabled in TestComplete (it is installed and enabled by default).

Declaration

TestObj.FindElements(Selector)

TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
Selector [in]    Required    Object    
Result An array of the tested objects.

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 parameter:

Selector

Specifies the search condition. Can be either an XPath expression or a CSS selector.

XPath
  • The search expression must comply with the XPath rules. For more information on them, see XPath documentation. For instance, you can find XPath Reference in the MSDN Library.

  • The search expression must contain the names of HTML elements and attributes. Do not use Name Mapping aliases.

  • The element and attributes names in the search expression are case-insensitive. That is, you can specify them either in the upper case, or in the lower case.

    However, the element and attribute values, which you specify in the search expression, are case-sensitive. For instance, image1.PNG, IMAGE1.PNG and Image1.png are different values for XPath. The search expression must specify values in the same case, in which they are specified in the source code of the page.

  • In search expressions, you can use XPath 1.0 functions like contains, substring, starts-with and others.

    XPath 2.0 functions (like lower-case(), matches() and others), Microsoft extensions and other third-party extensions to XPath functions are not supported.

The table below contains some examples of XPath expressions:

Expression Description
//IMG[contains(@src, 'plusBtn.gif')] All IMG elements whose src attribute contains the plusBtn.gif substring.
//A[starts-with(@href, 'javascript:')] All A elements whose href attribute starts with the javascript: prefix.
//DIV[@class='CodeExample']/P[1] All P elements that are located at the first position within DIV elements whose class attribute is set to CodeExample.
//*[@align='center'] All web page elements that have the align attribute set to center.
//UL/* A collection of direct children of any UL element.
//UL[@id='ProductList']/LI[last()] The last LI element within the UL element that has the id attribute set to ProductList.
//DIV//A All A elements that are descendants of any DIV element.
CSS selector

A search expression containing one or more CSS selectors. For more information on them, see CSS selector syntax documentation. For example, you can find it at www.w3.org/TR/selectors.

The table below demonstrates some examples of CSS selectors:

Selector Description
h3 All H3 elements.
#infobox An element with the ID "infobox".
.nav All elements with the class "nav".
div,p All DIV elements and all P elements.
div p All P elements inside DIV elements.
img.thmb All IMG elements of the "thmb" class.
a:link All unvisited links.
a[target=blank] All links that open in a new window.
a[src^="https"] All links whose src attribute value begins with "https".
li:first-of-type All LI elements that are the first item in their parent elements.
p:nth-child(2) All P elements that are located at the second position within their parent elements.

Result Value

An array of the objects that match the specified condition.

If no matching object has been found, the method returns an empty array.

Example

The example below shows how you can use the FindElements method to find all web elements that match the XPath and post their innerText property to the test log:

JavaScript

function FindElements_Example()
{
  …
  var browser = Sys.Browser();
  var page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore*");
  var articles = page.FindElements("//article");

  if (articles.length > 0)
  {
    for (var i = 0; i < articles.length; i++)
    {
      Log.Message(articles[i].innerText);
    }
  }
  else
  {
    Log.Message("No ARTICLE element is found on the page.");
  }
  …
}

JScript

function FindElements_Example()
{
  …
  var browser = Sys.Browser();
  var page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore*");
  var articles = page.FindElements("//article").toArray();

  if (articles.length > 0)
  {
    for (var i = 0; i < articles.length; i++)
    {
      Log.Message(articles[i].innerText);
    }
  }
  else
  {
    Log.Message("No ARTICLE element is found on the page.");
  }
  …
}

Python

def FindElements_Example():
  …
  browser = Sys.Browser();
  page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore*")
  articles = page.FindElements("//article")

  if (len(articles) > 0):
    for i in range (0, len(articles)):
      Log.Message(articles[i].innerText)
  else:
    Log.Message("No ARTICLE element is found on the page.")
  …

VBScript

Sub FindElements_Example()

  Set browser = Sys.Browser()
  Set page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore*")
  articles = page.FindElements("//article")

  If UBound(articles) > 0 Then
    For i = 0 To UBound(articles)
      Log.Message(articles(i).innerText)
    Next

  Else
  Log.Message("No ARTICLE element is found on the page.")

  End If

End Sub

DelphiScript

function FindElements_Example();
var browser, page, articles, i;
begin
  …
  browser := Sys.Browser();
  page := browser.Page('*services.smartbear.com/samples/TestComplete*/smartstore*');
  articles := page.FindElements('//article');

  if VarArrayHighBound(articles, 1) > 0 Then
  begin
    for i := 0 to VarArrayHighBound(articles, 1) do
      Log.Message(articles[i].innerText);
  end
  else
    Log.Message('No ARTICLE element is found on the page.');
  …
end;

C++Script, C#Script

function FindElements_Example()
{
  …
  var browser = Sys["Browser"]();
  var page = browser["Page"]("*services.smartbear.com/samples/TestComplete*/smartstore*");
  var articles = page["FindElements"]("//article");

  if (articles["length"] > 0)
  {
    for (var i = 0; i < articles["length"]; i++)
    {
      Log["Message"](articles[i]["innerText"]);
    }
  }
  else
  {
    Log["Message"]("No ARTICLE element is found on the page.");
  }
  …
}

See Also

FindElement Method (Page Objects)
Page Object

Highlight search results