Adrian Jimenez
Adrian Jimenez

Reputation: 1167

How to create xpath using attributes of different elements for ios app

I have a mobile ios app, which generates elements dynamically, i am struggling on clicking some checkboxes because they do not have an id or value like a common checkbox in web application, please look at the image below. (sorry i can't show all image, but app is in development)

enter image description here

Appium inspector gives me the xpath, but i do not like it because it changes depending on which elements are seen on the screen, since i have to scroll down the page, depending on the device, sometimes some checkboxes are displayed sometimes they are not, so the [4] element for example will change.

I want to know if there is a way to click the checkbox by concatenating the text adjacent to the checkbox, so that way it does not matter how many checkboxes are displayed on screen, it will always check by text.

enter image description here

so at the end i have an xpath something like:(//XCUIElementTypeButton[@name="checkbox unselected"])[and text='The hydraulic system was operational']

Upvotes: 0

Views: 1993

Answers (1)

Alexis
Alexis

Reputation: 463

If you were sure that you had an structure where every checkbox is followed by a label/text, you could then look for that label, then look for the first preceding-sibling.

//XCUIElementTypeStaticText[@name="The hydraulic system was operational"]/preceding-sibling::XCUIEelementTypeButton[1]

However, this may not work seeing your structure. It seems like checkboxes appear in the same order as the labels, so you could then get the relative index of those labels, and then get checkbox by index. Something like:

//label/../checkbox[count(label/preceding-sibling::LabelType)+1]

//XCUIElementTypeStaticText[@name="The hydraulic system was operational"]/../XCUIEelementTypeButton[count(//XCUIElementTypeStaticText[@name="The hydraulic system was operational"]/preceding-sibling::XCUIElementTypeStaticText)+1]

Anyway, none of this solutions are the best. You should ask the developers to put proper attributes.

Upvotes: 1

Related Questions