How to use xPath as a browser action selector, which is changing dynamically

Certified Senior Developer

I am developing browser automation for a website, using xPaths as the element selector for browser interactions. Whenever the website I am working on gets updated, the element xPaths change randomly. Is there any way to overcome this issue, or do I have to test and modify the browser action xPaths every time there is an update to the website?

  Discussion posts and replies are publicly visible

Parents Reply
  • +1
    Certified Senior Developer
    in reply to Hirushiharan

    Hi  ,

    Whenever possible, try to use unique attributes in your XPath expressions. Instead of relying solely on the position or structure of an element, use attributes like id or class that are less likely to change during updates. Utilize XPath functions to create more flexible and resilient expressions. Functions like contains(), starts-with(), and ends-with() can be helpful like below.

    //div[contains(@class, 'partialClassName')]

    //input[starts-with(@id, 'prefix')]

    if you use full xpath, It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.

Children