Example: Locating Siblings of a Control

This topic describes how you can locate a control, which does not provide any meaningful attributes that can be used in locators, when a stable locator for a sibling of the control is available.

Assume that you have already identified the control Item 0.0, which has the following stable locator:
/BrowserApplication//BrowserWindow//DIV[@textContent='Item 0.0']
If you know that Item 0.0 has a following-sibling of the type a, you can use the following code to build a stable locator for the sibling:
/BrowserApplication//BrowserWindow//DIV[@textContent='Item 0.0']/following-sibling::a
You can also use the sibling approach to identify text fields. Text fields often do not provide any meaningful attributes that can be used in locators. By using the label of a text field, you could create a meaningful locator for the text field, because the label is the best identifier for the text field from the perspective of a tester. You can easily use the label as a part of the locator for a test field by using the sibling approach. For example, if the text field is a preceding-sibling of a label with the text User Name, you can use the following locator:
/BrowserApplication//BrowserWindow//DIV[@textContent='User Name']/preceding-sibling::input[@type='text']