Two object have same Xpath in iOS


#1

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'])]


#2

does anyone have a solution?


#3

@Katalon_Studio
@Marek_Melocik
@Chris_Trevarthen


#4

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:

Then you can play around with the xpath to find a simpler one that works for you using https://www.freeformatter.com/xpath-tester.html

Hope this helps,

Chris


#5

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!


#6

@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


#7

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


#8

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


#9

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


#10

on iOS i’m testing an app, not browser


#11

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