Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Web and API Testing

Locators and waiting

Choose stable locators (id, css, xpath, data attributes) and wait for the page properly in Robot Framework, so web tests stop failing at random.

Two things decide whether your web tests are trustworthy: how you find elements and how you wait for them. Bad locators break when a designer moves a button. Missing waits make tests fail on a slow day and pass on a fast one. Tests like that are called flaky, and a flaky suite is one nobody believes.

This lesson covers the locator strategies of SeleniumLibrary, a ranking of which to prefer, the waiting keywords, and the habits that remove most flakiness. As before, the real keywords are shown read-only and the runnable code is a Python stand-in.

Locator strategies

A locator is a string. Robot reads the prefix before : or = to decide how to search.

Locator Finds Example
id: Element id id:email
name: Name attribute name:q
css: CSS selector css:form .btn-primary
xpath: XPath expression xpath://tr[td='Ada']
link: Link by exact text link:Sign out
partial link: Link by part of text partial link:Sign
class: Class name class:alert
tag: Tag name tag:h1

Without a prefix, Robot tries id and name; with // at the start it assumes XPath. Always writing the prefix is clearer.

Which locator to prefer

Rank them by how likely they are to survive a redesign:

  1. A dedicated test attribute, for example data-test="submit" found with css:[data-test="submit"]. Developers add it for testing and do not change it for styling.
  2. A stable id such as id:login.
  3. A short CSS selector that describes meaning, not layout.
  4. XPath only when you must walk between elements (find the row that contains a name, then its Delete button).
  5. Never a long absolute path like /html/body/div[2]/div/div[3]/button. It breaks on the next layout change.

Stable to fragile

Test attribute data-test, made for tests.
Id Unique and usually stable.
Short CSS Describe meaning.
XPath Only to move between elements.

A path from the page root is the most fragile choice.

1 / 2

Waiting the right way

A browser test is a race between your script and the page. The page loads data, runs scripts and animates. If the script clicks before the button exists, Selenium raises an error. There are three ways to deal with it.

  • A fixed sleep (Sleep 5s). Wasteful when the page is fast, too short when it is slow. Avoid it.
  • An implicit wait, set once with Set Selenium Implicit Wait 5s. Every find retries for up to 5 seconds. Handy but coarse.
  • An explicit wait, which waits for one specific condition, then continues the moment it is true. This is the best choice.
TEXT
Click Button    id:search
Wait Until Element Is Visible    css:.results    timeout=10s
Element Should Contain           css:.results    3 results

The Wait Until ... family includes Wait Until Page Contains, Wait Until Element Is Enabled, Wait Until Page Does Not Contain Element (good for spinners) and the general Wait Until Keyword Succeeds, which retries any keyword:

TEXT
Wait Until Keyword Succeeds    5x    1s    Page Should Contain    Order confirmed

Read it as "try up to 5 times, one second apart".

The default timeout for waiting keywords is 5 seconds, set with Set Selenium Timeout. Raise it for slow environments rather than adding sleeps.

A retry stand-in

Wait Until Keyword Succeeds is just a retry loop. This stand-in makes the idea concrete with a fake page that becomes ready after a few polls. It uses a counter instead of real time, so the output is the same every run.

Python Stand-in: wait until a condition is true

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The slow page that needs nine polls fails with a clear message, which is exactly what a real timeout should do: fail with what it waited for, not a mystery.

Choosing a locator in code

You can also model the ranking. This stand-in takes a list of candidate locators for one element and picks the most stable one by a score. Real tools do not do this for you, but writing it down makes the priorities concrete.

Python Stand-in: rank locators

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Habits that remove flakiness

  • Wait for a condition, never for a number of seconds.
  • Put locators in variables or in resource files so one change fixes all tests.
  • Keep tests independent: each creates its own data, as covered in data-driven testing.
  • Take a screenshot on failure (Run Keyword If Test Failed Capture Page Screenshot in the teardown), so the report shows what the page looked like.
  • Run the suite many times when a test is suspect; a test that fails once in twenty runs is flaky, not "mostly fine".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which locator is usually the most stable?

    A test attribute is added for tests and is not touched by styling changes.
  2. 2. What is the problem with Sleep 5s?

    A fixed sleep cannot adapt: it is either too long or too short.
  3. 3. What does Wait Until Keyword Succeeds 5x 1s Page Should Contain Done do?

    It repeats the keyword until it passes or the retries are used up.
  4. 4. How do you tell Robot to use a CSS selector?

    The prefix before the colon selects the strategy; a leading // means XPath.

Summary

  • Locators have strategies (id:, css:, xpath:, link:, ...); prefer test attributes and ids.
  • Avoid long absolute XPath.
  • Use explicit waits (Wait Until ...) instead of sleeps.
  • Wait Until Keyword Succeeds retries any keyword.
  • Flaky tests come from timing, fragile locators and shared data.

Next: Browser Library (Playwright)