I am currently automating some tasks with selenium and I faced a challenge where an element would render each time with a slightly different ID - the first part would be the same, but the ending would be different, such as id=navbarTop_13123
. Part navbarTop_ would always be the same, but the ending different.
Partial ID Selectors
Partial ID selectors are a powerful feature of CSS selectors that allow you to select elements based on a partial match of their id attribute. This can be useful when the id attribute of an element contains a dynamic or changing value, and you need to select the element based on only part of the id attribute.
To select an element with a partial id, you can use the following syntax:
|
|
So to always find my element even though the ending part would be different, I have used the following syntax:
|
|
This selector will select an element with an id that contains the text “navbarTop_”, regardless of where in the id attribute the text appears.
What is even better, CSS Selector offers options for even more dynamic searching:
|
|
It is very handy knowing these options exist. You can find more at W3 School reference.