Dynamic elements are those whose attributes (like ID or class name) change upon page refresh or a user action. Handling them requires using robust and reliable locators:
- Partial Link Text: Use
By.partialLinkText()if only a part of the link’s text is static. - Custom CSS Selectors: Create a CSS selector that uses a combination of static attributes.
- XPath: Use XPath with functions like
contains(),starts-with(), orends-with()to match a part of a dynamic attribute’s value. - Relative Locators (Selenium 4+): The new
withTagName().near()and other relative locators are excellent for handling elements whose position relative to a static element remains constant.