Writing Effective XPath

Learn the tips and tricks for writing effective XPath, using our course knowledge so far.

We'll cover the following

Tips for writing effective XPath

The practices below can help us write an effective XPath. They have already been discussed in the course so far:

  • Always use relative XPath; it starts with (//).

  • Look for unique id or name attributes, or combinations using XPath operators (or, and, etc ).

  • Use function or a combination of functions (contains, text(), etc.) and attributes for writing the XPath expression.

  • Look for sibling, ancestors, descendants or parents to identify the web element uniquely.

  • Avoid using text comparison to locate as much as possible, since any change in the text will break the XPath.

  • Avoid using any dynamic attribute to locate the XPath. Rather, use the techniques discussed in the earlier lessons to identify the element uniquely.

  • Avoid using absolute XPath (/), always use relative XPath (//).

Get hands-on with 1200+ tech skills courses.