# How to iterate over child elements using TestObject as XPath envelope

**URL:** <https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598>\
**Category:** Tips and Tricks\
**Tags:** tips-tricks, katalon-studio\
**Created:** [October 7, 2018, 5:53am UTC](https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598 "2018-10-07T05:53:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [October 7, 2018, 5:53am UTC](https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598/1 "2018-10-07T05:53:22Z")

</div>

I have made a demo at

- [https://github.com/kazurayam/How\_to\_iterate\_over\_child\_elements\_using\_TestObject\_as\_XPath\_envelope](https://github.com/kazurayam/How%5C_to%5C_iterate%5C_over%5C_child%5C_elements%5C_using%5C_TestObject%5C_as%5C_XPath%5C_envelope)

* * *

## How to run the demo

1. download the zip file of this project from the [Releases](https://github.com/kazurayam/How_to_iterate_over_child_elements_using_TestObject_as_XPath_envelope/releases) page, unzip it.
2. start Katalon Studio, open the project
3. open `Test Cases/TC1_iterate_over_Katalon_Discussions`, and run it with any browser

## Input to the demo

The TestCase opens [http://forum.katalon.com/discussions](http://forum.katalon.com/discussions). There you will find a list of 37 discussions.

## Output from the demo

The TestCase iterates over the list of discussions and print the titles like this:

```auto
10-07-2018 01:33:23 PM - [INFO] - >>> 1: Visual Testing In Katalon Studio...10-07-2018 01:33:23 PM - [INFO] - >>> 37: Query on: Running remotely and paralel thread, Merging teams code in Git,Ex/Import of KatalonProject

```

## How the demo works

### TestObjects made

1. [`Page_Recent Discussions/Content.ul_class-Disscussions`] with xpath  
`//div[@id='Content']/div/ul`
2. [`Page_Recent Discussions/Content.(li_Item)`] with xpath  
`//li`
3. [`Page_Recent Discussions/Content.(a_Title)`] with xpath  
`//div[contains(@class, 'Title')]/a`

### TestCase made

I made 1 TestCase [`TC1_iterate_over_Katalon_Discussions`](https://github.com/kazurayam/How_to_iterate_over_child_elements_using_TestObject_as_XPath_envelope/blob/master/Scripts/TC1_iterate_over_Katalon_Discussions/Script1538870798504.groovy). I would copy its portion and past it here:

```auto
WebUI.navigateToUrl('http://forum.katalon.com/discussions')
WebDriver driver = DriverFactory.getWebDriver()// read XPath fragments
String ulSelector = 
    findTestObject('Object Repository/Page_Recent Discussions/Content.ul_class-Discussions').findPropertyValue('xpath')
String liSelector = 
    ulSelector + findTestObject('Object Repository/Page_Recent Discussions/Content.(li_Item)').findPropertyValue('xpath')

// obtain the number of Discussion rows
int rowCount = driver.findElements(By.xpath(liSelector)).size()
WebUI.comment(">>> rowCount=${rowCount}")// iterate over Discussions in the Forum

for (int pos = 1; pos <= rowCount; pos++) {
    // selector to the Title of each Discussion
    String titleSelector = liSelector + "[position()=${pos}]" +
        findTestObject('Object Repository/Page_Recent Discussions/Content.(a_Title)').findPropertyValue('xpath')
    TestObject to = new TestObject().addProperty('xpath', ConditionType.EQUALS, titleSelector)    
    WebUI.verifyElementPresent(to, 1)
    String title = WebUI.getText(to)
    WebUI.comment(">>> ${pos}: ${title}")
}
...

```

The following line is the core part of this script:

```auto
String liSelector = 
    ulSelector + 
    findTestObject('Object Repository/Page_Recent Discussions/Content.(li_Item)').findPropertyValue('xpath')
...
for (int pos = 1; pos < rowCount; pos++) {
    String titleSelector = 
        liSelector +
        "[position()=${pos}]" +
        findTestObject('Object Repository/Page_Recent Discussions/Content.(a_Title)').findPropertyValue('xpath')
    ...
}
...

```

Generated tilesSelectors will be as follows:

- `//div[@id='Content']/div/ul//li[position()=1]//div[contains(@class, 'Title')]/a`
- `//div[@id='Content']/div/ul//li[position()=2]//div[contains(@class, 'Title')]/a`
- `//div[@id='Content']/div/ul//li[position()=3]//div[contains(@class, 'Title')]/a`
- `//div[@id='Content']/div/ul//li[position()=4]//div[contains(@class, 'Title')]/a` …
- `//div[@id='Content']/div/ul//li[position()=37]//div[contains(@class, 'Title')]/a`

## Conclusion

1. In the demo I used **TestObject just as an Envelope of XPath selector literal**.
2. TestObject is designed primarilly for the Katalon Studio features: “Record Web” and “Spy Web”. But you can modify/refactor the generated TestObject manually, and it is highly recommended.
3. I had **no XPath literals hard-coded and scattered in the sample TestCase scripts. All the XPath literals are externalized into the Object Repository. This would make code maintenance easier.** It is good to have magical values centralized in the Object Repository. It would be nightmare if you scatter XPath literals all over the TestCase scripts.
4. This sample shows you that you can introduce “searching for child elements” and “something dynamic” by a bit tricky coding. You construct XPath literals dynamically for yourself using the XPath fragments stored in the TestObjects as Envelope.

Beginners might not be able to use this approach, but well-trained programmers like you would easily see this idea.

**Thanks to** Marek Melocik for the original idea. It was posted at

> [@How to verify text is present but only within a given DOM element?](http://forum.katalon.com/t/how-to-verify-text-is-present-but-only-within-a-given-dom-element/6643):
>
> I can use verifyTextPresent but that looks throughout the entire DOM. How do I limit the search to, for instance, a given div ? I have some text which appears in multiple places and I want to be sure that the text I’m finding is the text I’m interested in . I feel sure this must be a very common requirement but I can’t see it mentioned in the tuts etc.

---

<div class="post-metadata">

**Author:** ![Andrej\_Podhajsky](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrej_podhajsky/32/1055_2.png) [@Andrej\_Podhajsky](https://forum.katalon.com/u/Andrej_Podhajsky)\
**Post date:** [October 7, 2018, 6:44am UTC](https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598/2 "2018-10-07T06:44:10Z")

</div>

super +1

---

<div class="post-metadata">

**Author:** ![preeti.dodani](https://avatars.discourse-cdn.com/v4/letter/p/ce73a5/32.png) [@preeti.dodani](https://forum.katalon.com/u/preeti.dodani)\
**Post date:** [June 11, 2020, 6:44pm UTC](https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598/3 "2020-06-11T18:44:58Z")

</div>

In my case, I want to get the child data by using parent Index. So by updating TestObject property i.e.

String dynamicIdPath = “[//android.widget.LinearLayout](https://android.widget.linearlayout/)[contains(@index,‘2’)]/\*”

child.addProperty(“xpath”,ConditionType.EQUALS,dynamicIdPath,true)

Its is not working in my case. Is there is any way to get child data using parent TestObject i.e. using index attribute

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [June 11, 2020, 8:55pm UTC](https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598/4 "2020-06-11T20:55:28Z")

</div>

I am not capable of helping you as I have very little experience of Mobile testing. You may ask Mobile testing experts ( specifically @Chris_Trevarthen ) for help.

You shared just 2 lines of code fragment and wrote “it is not working”. That’s not enough. We can not see how your target application looks like. We can not see what you expected to see and what you actually saw. We need to see whole lines of your test script rather than 2 lines of code fragment. We need to see the log you got.

Please have a look at the following post and provide more information about your issue.

[[TIP] How To Help Us Help You!](http://forum.katalon.com/t/tip-how-to-help-us-help-you/12919)

---

<div class="post-metadata">

**Author:** ![Chris\_Trevarthen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/chris_trevarthen/32/358_2.png) [@Chris\_Trevarthen](https://forum.katalon.com/u/Chris_Trevarthen)\
**Post date:** [June 25, 2020, 3:09am UTC](https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598/5 "2020-06-25T03:09:27Z")

</div>

Hi @preeti.dodani,

The best way I’ve found to get a child element is to build on the xpath of its parent. I think you’re on the right path.

For some more info on xpath syntax, you can check out this site:

> **[XPath Syntax](https://www.w3schools.com/xml/xpath_syntax.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

You can also try out various xpaths against your document with this xpath tester:

> **[Free Online XPath Tester / Evaluator - FreeFormatter.com](https://www.freeformatter.com/xpath-tester.html)**
>
> This free online XPath tester lets you test your expressions/queries against an XML file

To get the XML as Katalon/Appium sees it, you can follow these instructions:

> [@Unable to click/tap on a button in Android WebView](http://forum.katalon.com/t/unable-to-click-tap-on-a-button-in-android-webview/20135/7):
>
> Hi @Sravya_Batchu, That’s a lot of good information, thank you! I’m curious what Katalon and Appium think are on the screen at the time you switch to the WebView. If you want to see some more detail about exactly what Appium/Katalon detects on the screen you can log out the XML contents of the screen. You should be able to do that by putting the following import statements at the top of your test case: import com.kms.katalon.core.logging.KeywordLogger import com.kms.katalon.core.mobile.keywor…

Hope this helps,

Chris

---

<div class="post-metadata">

**Author:** ![albert.vu](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/albert.vu/32/20595_2.png) [@albert.vu](https://forum.katalon.com/u/albert.vu)\
**Post date:** [August 8, 2022, 7:32am UTC](https://forum.katalon.com/t/how-to-iterate-over-child-elements-using-testobject-as-xpath-envelope/13598/6 "2022-08-08T07:32:03Z")

</div>

Archived on August 8, 2022, 7:32am UTC


