Selectors

How CSS and XPath selectors are detected, and how to write selectors that survive page changes.

CSS or XPath

Every CSS or XPath Selector field accepts either syntax. The runner decides which one you wrote from how the selector starts, so you do not pick a type.

  • XPath: starts with /, (, ./ or ../, for example //button[text()='Save'] or (//li)[2]
  • XPath: starts with an axis such as descendant:: or following-sibling::
  • XPath: starts with the prefix xpath=, which is removed before the lookup
  • CSS: everything else, for example #login or .cart button
  • CSS: the prefix css= is accepted and removed before the lookup
#login-button
css=[data-test='cart-total']
//button[normalize-space()='Place order']
xpath=(//ul[@id='results']/li)[1]

Choosing between them

Both find the same elements in most cases. Use the one that expresses the element most simply:

  • CSS: shorter and easier to read for ids, classes and attributes
  • CSS: cannot match an element by its visible text
  • XPath: can match by text, for example //a[normalize-space()='Pricing']
  • XPath: can move up to a parent or across to a sibling, for example //label[text()='Email']/following-sibling::input

One selector per step

Each step uses a single selector. There is no list of fallback selectors tried in order. If a step needs to work with two page layouts, fix the selector or add a step condition that checks which layout is shown.

When the selector matches several elements, the step acts on the first match in page order.

Waiting for elements

Steps that act on an element wait for it to appear before failing. The wait is set by Element Timeout in the suite's test defaults, from 5 to 60 seconds, with a default of 5 seconds.

Writing stable selectors

Selectors break when they depend on details that change between releases. In order of preference:

  • An id, for example #checkout-button
  • A dedicated test attribute, for example [data-test='checkout']
  • Another stable attribute, for example input[name='email'] or a[href='/pricing']
  • Visible text with XPath, for buttons and links whose label is fixed
#checkout-button
[data-test='checkout']
input[name='email']
//button[normalize-space()='Checkout']

Selectors to avoid

Generated class names, such as .css-1x2y3z or .sc-a1b2c3, change on every build. Long positional paths, such as /html/body/div[3]/div[2]/span or div > div:nth-child(4), break when any element is added above the target.