Share this page

# Iterate over links in a web page

This page provides examples for iterating links over a web page. This is useful when you have a page that contains a series of links, and 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.

In this topic:

## Use a parameter and a For Loop step

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

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_2.zip

To use a parameter and a For Loop step to iterate over links:

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

1. Record the following steps into the For Loop:

   1. Add an Evaluate JavaScript step. Expand Arguments, and in the Code dropdown box enter the following code using the JavaScript editor .

      ArgsContext.linkText = ArgsContext.TC.getParam("LinkText");

   2. Click Link 1 - this is an example.

   3. Click the Back to Links page button.

   4. 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.linkText + "\"]");`

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

4. Replay the script.

Back to top

## Use the XPath property, JavaScript, and a For Loop step

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.

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_2.zip

To dynamically click links using XPath property:

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 Work with the ArgsContext object.

4. Replay the script.

Back to top

## Use the XPath DOM structure, JavaScript, and a For Loop step

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.

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_2.zip

To dynamically click links using XPath DOM structure:

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

To use descriptors to iterate over links:

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_2.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 the Edit button 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

https://admhelp.microfocus.com VuGen,Professional Performance Engineering,Performance Engineering for Developers 8942
Tips to improve your search results... OpenText™ Virtual User Generator Help Center OpenText™ VuGen Help 26.1 All View Local Help Center View Online Help Center Version: