Common Issues Dynamic Locators - dn0000001/test-automation GitHub Wiki
When you use Dynamic locators, a common issue is to use methods that will use the locator. In these cases, the locator will not be correct as the substitutions will not have been applied when the getLocator() method is used.
There is no workaround possible to make getLocator() apply the substitutions. This is due to how the By class stores (or does not store the locator information) and it does not expose any method to retrieve the string used to create the By locator.
In these cases, you should store the locator as a string in the page object class. Then, you can use the LocatorUtils.processForSubstitutions method to use the locator string to create the correct By locator at run-time.
Update: This may have been true in the past but it should no longer be the case. The only case in which the locator will not be correct is if the substitutions are missing or the page object containing the component was not initialized.