Iterate over links in a Web page

Let's suppose you have a Web page that contains a series of links. You would like to create a script that sequentially clicks each link.

You can record a script by manually clicking each link.

Alternatively, you can accomplish this task dynamically using one of the methods in this topic.

Use a parameter and a For Loop statement

You can use a parameter and a For Loop step to automatically iterate over links in your TruClient script.

Try it out!

The following table summarizes the pages and scripts used in the example:

Example description HTML pages Page Description Example scripts
Iterate over links in a Web page

links.html

empty.html

 

List of links

Landing page

Links1.zip

  1. Add a parameter to your script ("LinkText") and create entries for each link name. For details on creating parameters, see Use Parameters to Vary Arguments.

  2. Record navigation to links.html.
  3. Add a For Loop step to your script and set the i variable to the number of links on your page. In this example, i is set to 8.

  1. Record the following steps into the For Loop:

    1. Click Link 1 - this is an example.
    2. Click the Back to Links page button.
    3. Stop recording.
  2. In the Object section, change the ID method on the Link 1 - this is an example from Automatic to JavaScript.
  3. Add the following code to the JavaScript text under the Object section:

    evalXPath("//li[text()=\"" + ArgsContext.TC.getParam("LinkText") + "\"]");

    Note: ArgsContext enables you to refer to arguments defined outside the argument section. For details, see Working with the ArgsContext object.

  4. Replay the script.

Back to top

Use the XPath property, JavaScript and a For Loop

You can also use the For Loop step, the XPath property, and JavaScript to develop a script section that dynamically clicks links in a script.

Try it out!

The following table summarizes the pages and scripts used in the example:

Example description HTML pages Page Description Example scripts
Iterate over links in a Web page

links.html

empty.html

List of links

Landing page

Links2.zip

  1. Record navigation to links.html.
  2. Add a For Loop step to your script and set the i variable to the number of links on your page. In this example, i is set to 0. Init value is set to 1.

  1. Record the following steps into the For Loop:

    1. Click Link 1 - this is an example.
    2. Click the Back to Links page button.
  2. Change the ID method on the Link 1 - this is an example from Automatic to JavaScript.
  3. Add the following code to the JavaScript text under the Object section:

    evalXPath("//li[text()=\"Link " + ArgsContext.i + " - this is an example\"]");

    TruClient evaluates the statements and then runs an XPath query on the DOM to find any list item with text equals to the value fetched from parameter “LinkText”

    Note: ArgsContext enables you to refer to arguments defined outside the argument section. For details, see Working with the ArgsContext object.

  4. Replay the script.

Back to top

Use the XPath DOM Structure, JavaScript and a For Loop

You can also use the For Loop step, the XPath DOM structure, and JavaScript to develop a script section that dynamically clicks links in a script.

Try it out!

The following table summarizes the pages and scripts used in the example:

Example description HTML pages Page Description Example scripts
Iterate over links in a Web page

links.html

empty.html

List of links

Landing page

Links3.zip

  1. Record navigation to links.html.
  2. Add a For Loop step to your script and set the i variable to the number of links on your page. In this example, i is set to 0. Init value is set to 1.

  3. Record the following steps into the For Loop:

    1. Click Link 1 - this is an example.
    2. Click the Back to Links page button.
  4. Change the ID method on the Link 1 - this is an example from Automatic to JavaScript.
  5. Add the following code to the JavaScript text under the Object section:

    evalXPath("//html/body/ul/li[" + ArgsContext.i + "]/a");

    TruClient evaluates the statements and then run XPath query on the DOM to find any list item with text equals to the value fetched from parameter “LinkText”

  6. Replay the script.

Note: ArgsContext enables you to refer to arguments defined outside the argument section. For details, see Working with the ArgsContext object.

Back to top

Use descriptors

You can use descriptors and the object ordinal feature to automatically iterate over links in a Web page in your TruClient script.

Try it out!

The following table summarizes the pages and scripts used in the example:

Example description HTML pages Page Description Example scripts
Iterate over links in a Web page

links.html

empty.html

 

List of links

Landing page

Links4.zip

  1. Record navigation to links.html.
  2. Add a For Loop step to your script and set the i variable to the number of links on your page. In this example, i is set to 0. Init value is set to 1.

  3. Record the following steps into the For Loop:

    1. Click Link 1 - this is an example.
    2. Click the Back to Links page button.
  4. Change the ID method on the Link 1 - this is an example from Automatic to Descriptors.
  5. Click "Click here to edit" to open the Descriptors Editor.
  6. Change the operator of the text property from "equals" to "equalsIgnoreDigits". This forces TruClient to ignore the digits while searching for the DOM object.
  7. From the Descriptors editor, select the object ordinal JS instead of plain text. Enter the following code:

    ArgsContext.i

  8. Replay the script.

Back to top