Updated: Jan 31
There is a lot of talk about the announcement of the "new" feature relative locators or friendly locators for Selenium 4, a feature that is more mature and used in frameworks like taiko https://taiko.gauge.org/#relativeselector or helium https://heliumhq.com/docs/api_documentation
The possibilities are:
above: Web element appears above specified web element.
below: Web element appears below the specified web element.
near: Web element is at most 50 pixels away from the specified element.
toLeftOf: Web element appears to the left of the specified element.
toRightOf: Web element appears to the right of the specified element
All of these methods accept a By or a WebElement.
The actual use for selenium is really simple
The main idea is to find an element by the position of another, discarding multiple matches, and select the correct one, like choose the input that is near the button login.
How does it work ?
Advancement or just a feature
This feature is an advancement for developers so they can access nearby web elements with fewer lines. Also is an interesting idea for validate UI, validating element positions.
The disadvantages at the moment are that these locators do not work with overlapping elements or elements that have the position changed by css because Selenium decides the location of the element based on the HTML present in the source code, irrespective of CSS Values.
This behavior will give unexpected results in finding elements and make your tests to fail in some cases.
Related locators seem like a nice feature that needs more improvement for work like helium or taiko for being production-ready, anyway they add nothing new that you can not do with regular xpath.