Xpath Troubleshooting
    • Dark
      Light

    Xpath Troubleshooting

    • Dark
      Light

    Article summary

    What makes a good Xpath?

    When writing our Xpaths, we want to remember a few things in order to increase the chances of it working for our automation.

    A good identifier is:

    • Unique
    • Resilient
    • Short as possible

    To locate a single element, a Xpath will need to have only one element it can select from the entire web page (unique). 

    To make it easier to read and maintain for users, we will want to make them as short as possible

    We need to write Xpath in a way that the path is still useable if the web page changes in any way (resilient).

    Top 6 reasons Xpaths fail, and how to fix them.

    1. Incorrect XPath Expression

    Check the XPath expression and make sure it is correct.

    Solution: Try using the Chrome or Firefox console to test the XPath expression.


    2. Dynamic Content

    If the webpage contains dynamic content, the XPath may not work as expected.

    Solution 1: Try using Relative XPaths instead of Absolute XPaths.

    Solution 2: Always refresh the page and check the identifier still works (IDs often change – especially ones that contains something that doesn’t look real).


    3. Element Position

    If the position of an element changes on the webpage, the XPath may need to be updated.

    Solution: Try using unique attributes like "id" or "name" instead of position-based XPaths.


    4. Case Sensitivity

    XPath expressions are case-sensitive, so make sure that the elements and attributes are spelled correctly.

    Solution: Double check your spelling


    5. Special Characters

    Special characters like quotes and slashes need to be escaped in XPath expressions.

    Solution: Use backslashes () to escape special characters.


    6. Iframe / ShadowDOM

    Iframes and ShadowDOMs will "hide" identifiers within them.

    Solution 1: Write individual identifiers to expand each shadowRoot 

    Solution 2: Navigate the context to each embedded Iframe