Web Testing

Robot Framework Selenium: Browser Testing With SeleniumLibrary

Browser window with a targeting reticle for Selenium element selection

Robot Framework Selenium testing means using SeleniumLibrary, a popular external library that connects Robot Framework keywords to Selenium WebDriver. With it, a test can open a browser, fill forms, click buttons and verify page content using readable steps that the whole team can review.

What SeleniumLibrary Provides

SeleniumLibrary wraps Selenium WebDriver in a large set of ready-made keywords. Instead of writing Python code for every interaction, you call keywords such as Open Browser, Input Text and Page Should Contain, while the library handles WebDriver sessions, element lookup and screenshots on failure.

  • Browser control: opening, switching, resizing and closing browsers
  • Element interaction: typing, clicking, selecting and uploading files
  • Verification keywords for text, titles, elements and attributes
  • Waiting keywords that handle dynamic pages
  • Automatic screenshots embedded in log.html when a keyword fails

Installing SeleniumLibrary

Install the library into the same virtual environment as Robot Framework. The package name on PyPI is robotframework-seleniumlibrary, and pip also brings in the Selenium Python bindings it depends on.

python -m pip install robotframework robotframework-seleniumlibrary

You also need a browser such as Chrome, Firefox or Edge. Recent Selenium releases include Selenium Manager, which can locate or download a matching driver automatically, so manual driver setup is often unnecessary on a typical workstation.

Your First Browser Test

Import the library in the Settings section, define the target URL as a variable and write a test that opens the page, interacts with it and checks the result. Keeping locators and URLs in variables makes the suite easier to adapt to other environments.

*** Settings ***
Library           SeleniumLibrary
Suite Teardown    Close All Browsers

*** Variables ***
${URL}        https://example.com/login
${BROWSER}    chrome

*** Test Cases ***
Log In With Valid User
    Open Browser    ${URL}    ${BROWSER}
    Input Text      id:username    demo
    Input Password  id:password    ${PASSWORD}
    Click Button    Log in
    Page Should Contain    Welcome

Pass the password from the command line with --variable PASSWORD:secret instead of storing it in the file. Run the suite with robot as usual and review log.html to see each browser action in order.

Locator Strategies

SeleniumLibrary identifies elements with locators written as a strategy prefix followed by a value. Choosing stable locators is the single biggest factor in keeping browser suites reliable when the user interface changes.

StrategyExampleWhen to use
idid:usernameUnique, stable IDs are the most reliable choice
namename:emailForm fields with consistent name attributes
csscss:button.primaryFlexible selection by class or structure
xpathxpath://h1[text()="Sign in"]Text-based or complex relationships
linklink:Forgot password?Anchors identified by visible text

Ask developers to add dedicated test attributes such as data-testid where possible, and target them with a CSS locator. These attributes rarely change during visual redesigns.

Handling Waits and Dynamic Pages

Modern pages load content asynchronously, which is the most common source of flaky tests. Avoid fixed Sleep calls and use waiting keywords that poll until a condition is met or a timeout expires.

  • Wait Until Element Is Visible for elements that appear after loading
  • Wait Until Page Contains for text rendered by scripts
  • Wait Until Element Is Enabled before clicking buttons that start disabled
  • Set Selenium Timeout to adjust the default waiting time for a suite

Structuring Browser Suites

Long tests full of locators become hard to maintain. Move page-specific actions into user keywords inside resource files, one per page or feature, so tests describe user goals and locators live in a single place.

*** Keywords ***
Log In As
    [Arguments]    ${user}    ${password}
    Input Text        id:username    ${user}
    Input Password    id:password    ${password}
    Click Button      Log in

This pattern follows the same principles described in the Robot Framework keywords overview, where user keywords act as a readable layer above library keywords.

Running Headless in CI

Continuous integration servers usually have no display, so browsers run in headless mode. SeleniumLibrary accepts browser names such as headlesschrome and headlessfirefox, which makes switching between local and CI runs a matter of changing one variable.

SeleniumLibrary Compared With Browser Library

Browser library is another popular choice for web testing in the Robot Framework ecosystem. It is built on Playwright rather than Selenium and offers automatic waiting, browser contexts and network control, while requiring Node.js during installation.

AspectSeleniumLibraryBrowser library
EngineSelenium WebDriverPlaywright
Extra runtimeBrowser and driverNode.js and Playwright browsers
Waiting modelExplicit wait keywordsBuilt-in automatic waiting
MaturityLong history and wide adoptionNewer, actively developed

Debugging Failing Browser Tests

A failing browser test is easiest to diagnose from log.html, where the failing keyword, its arguments and an automatic screenshot appear together. Comparing the screenshot with the expected page state usually reveals whether the problem is timing, a changed locator or a real application bug.

  • Check the screenshot embedded under the failed keyword
  • Confirm the locator still matches exactly one element
  • Replace fixed sleeps with an appropriate wait keyword

Frequently Asked Questions

Is SeleniumLibrary part of the core framework?

No. SeleniumLibrary is an external library installed separately with pip. The core package includes standard libraries such as BuiltIn, Collections and OperatingSystem, but not browser automation.

Which browsers can Robot Framework Selenium tests use?

Any browser supported by Selenium WebDriver can be used, including Chrome, Firefox, Edge and Safari, as long as the matching driver is available on the machine running the tests.

How do I take a screenshot manually?

Call Capture Page Screenshot at any point in a test. The image is saved next to the log and embedded in log.html, which helps document important states.

How can I make browser tests faster?

Run headless, avoid fixed sleeps, reuse a logged-in session where it is safe and run suites in parallel with Pabot. Stable locators also reduce slow retries.

Can SeleniumLibrary test mobile web pages?

Yes. Browsers can be resized or started with device emulation options, and AppiumLibrary covers native mobile apps when full device automation is required.

For a search page that updates without a full reload, define what a completed search looks like before choosing a wait. The visible results area might exist before the response arrives, so wait for a specific result or an empty state message associated with the submitted search query. Use a locator that identifies that region uniquely. This makes a slow response and a genuine mismatch easier to distinguish in the test log.

Conclusion

Robot Framework Selenium suites combine SeleniumLibrary’s browser keywords with readable test data, stable locators and explicit waits. Start with one login flow, move page actions into resource files and run headless in CI. For the underlying syntax, the Robot Framework tutorial is a good companion.