# Two object have same Xpath in iOS

**URL:** <https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014>\
**Category:** Mobile Testing\
**Tags:** mobile-testing, katalon-studio\
**Created:** [November 8, 2019, 9:50am UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014 "2019-11-08T09:50:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fap](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fap/32/23869_2.png) [@fap](https://forum.katalon.com/u/fap)\
**Post date:** [November 8, 2019, 9:50am UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/1 "2019-11-08T09:50:24Z")

</div>

How can I solve the problem? I have two forms, on ios, with the same xpath. how to distinguish them?  
when katalon has to fill in the second form, it ends up adding text to the first one

`//XCUIElementTypeApplication/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeScrollView[1]/XCUIElementTypeOther[1]/XCUIElementTypeTextField[1][count(. | //*[@type = 'XCUIElementTypeTextField']) = count(//*[@type = 'XCUIElementTypeTextField'])]`

---

<div class="post-metadata">

**Author:** ![fap](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fap/32/23869_2.png) [@fap](https://forum.katalon.com/u/fap)\
**Post date:** [November 8, 2019, 10:47am UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/2 "2019-11-08T10:47:41Z")

</div>

does anyone have a solution?

---

<div class="post-metadata">

**Author:** ![fap](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fap/32/23869_2.png) [@fap](https://forum.katalon.com/u/fap)\
**Post date:** [November 8, 2019, 1:36pm UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/3 "2019-11-08T13:36:19Z")

</div>

@katalon.team  
@Marek_Melocik  
@Chris_Trevarthen

---

<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:** [November 9, 2019, 3:24am UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/4 "2019-11-09T03:24:30Z")

</div>

Hi @fap,

As far as I know, it’s not really possible for 2 objects on the same screen to have the same full xpath. They may share most of the xpath, but at the end, they will have a different index. So, in your example above, it would be something like this:

`//XCUIElementTypeApplication/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeScrollView[1]/XCUIElementTypeOther[1]/XCUIElementTypeTextField[1][count(. | //*[@type = 'XCUIElementTypeTextField']) = count(//*[@type = 'XCUIElementTypeTextField'])][1]`

and

`//XCUIElementTypeApplication/XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeScrollView[1]/XCUIElementTypeOther[1]/XCUIElementTypeTextField[1][count(. | //*[@type = 'XCUIElementTypeTextField']) = count(//*[@type = 'XCUIElementTypeTextField'])][2]`

I will say that your xpath above seems very complicated and is likely to break if even a small change to the layout occurs. In my tests, I try to make the xpath simple as possible by giving each element its own accessibility id in Xcode and then using that as the `name` attribute for the Test Object.

If you don’t have access to make a change to assign accessibility ids, then you can at least try to simplify the xpath by capturing the entire contents of the screen as XML. See this post for how to do that:

> [@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…

Then you can play around with the xpath to find a simpler one that works for you using [Free Online XPath Tester / Evaluator - FreeFormatter.com](https://www.freeformatter.com/xpath-tester.html)

Hope this helps,

Chris

---

<div class="post-metadata">

**Author:** ![Yaacov\_Silverstein](https://avatars.discourse-cdn.com/v4/letter/y/a3d4f5/32.png) [@Yaacov\_Silverstein](https://forum.katalon.com/u/Yaacov_Silverstein)\
**Post date:** [November 11, 2019, 8:46am UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/5 "2019-11-11T08:46:57Z")

</div>

You can try something like this, usually works for me:

Lets says this is the xpath that you have twice:

//div[@class=“test”]

I would use the following to get the second one:

(//div[@class=“test”)[2]

Let me know if it helps!

---

<div class="post-metadata">

**Author:** ![fap](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fap/32/23869_2.png) [@fap](https://forum.katalon.com/u/fap)\
**Post date:** [November 11, 2019, 9:40am UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/6 "2019-11-11T09:40:54Z")

</div>

@Chris_Trevarthen  
@Yaacov_Silverstein

yes, it’s true, the xpaths aren’t exactly the same, I know, the indexes are different, rightly, but the objects are not detected and the test goes wrong:

```
// XCUIElementTypeApplication / XCUIElementTypeWindow [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeScrollView [1] / XCUIElementTypeOther [1] / XCUIElementTypeTextField [1]

// XCUIElementTypeApplication / XCUIElementTypeWindow [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeOther [1] / XCUIElementTypeScrollView [1] / XCUIElementTypeOther [1] / XCUIElementTypeTextField [2]

```

if I try to build xpath differently using data as “value”, I get simple and different xpaths, but the test goes wrong because the objects are not found

```
// * [@ value = concat ('Enter the company name of ",",' company ')]

// * [@ value = concat ('Enter the, "'", 'e-mail of, "'", 'user')]

```

the objects in question, two text forms, do not have ids or even classes

---

<div class="post-metadata">

**Author:** ![Yaacov\_Silverstein](https://avatars.discourse-cdn.com/v4/letter/y/a3d4f5/32.png) [@Yaacov\_Silverstein](https://forum.katalon.com/u/Yaacov_Silverstein)\
**Post date:** [November 11, 2019, 11:45am UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/7 "2019-11-11T11:45:27Z")

</div>

@fap - understood, did you try using “(” before and after “)” as I added. when I have a similar problem, it resolves the issue and works and only finds one. You can test in Chrome, inspect element.

---

<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:** [November 11, 2019, 1:08pm UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/8 "2019-11-11T13:08:52Z")

</div>

> [@fap](#):
>
> the objects in question, two text forms, do not have ids or even classes

In iOS, the class of the element is in the `type` property and the id of the element is in the `name` property. If the element doesn’t have a `value`, you can also check the `label` property.

If Yaacov’s suggestion above about wrapping the xpath in parentheses before adding the index doesn’t work, can you share the XML contents of the screen (see my earlier post) so we can troubleshoot further?

– Chris

---

<div class="post-metadata">

**Author:** ![fap](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fap/32/23869_2.png) [@fap](https://forum.katalon.com/u/fap)\
**Post date:** [November 11, 2019, 1:32pm UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/9 "2019-11-11T13:32:49Z")

</div>

the properties of the object are these, I don’t have the property “name”

 ![13](https://europe1.discourse-cdn.com/katalon/original/2X/d/d610adf9bbd3321c0d5513d5fcb9bf0b434c804f.png)

---

<div class="post-metadata">

**Author:** ![fap](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/fap/32/23869_2.png) [@fap](https://forum.katalon.com/u/fap)\
**Post date:** [November 11, 2019, 2:02pm UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/10 "2019-11-11T14:02:10Z")

</div>

on iOS i’m testing an app, not browser

---

<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:** [November 11, 2019, 2:40pm UTC](https://forum.katalon.com/t/two-object-have-same-xpath-in-ios/36014/11 "2019-11-11T14:40:34Z")

</div>

Hi, the `name` property in iOS will be populated if the developers set the accessibility id for the element in Xcode. I would highly recommend this if possible so that you have a more direct way of accessing an element than a very long xpath.

In the screenshot you just shared, the combination of `type` and `value` may be enough to find the element, assuming that the `value` is unique across text fields.

– Chris
