Skip to content

SeleniumLibrary guide for Robot Framwork#

Original Author Ville Purhonen
Last Checked By Juha-Matti Hietala
Last Updated 16.01.2025

Web user interface testing is most likely one of the first things you will want to automate. It is also a good use case for using Robot Framework, as you typically want to emulate graphical user interface inputs that an actual user would. Doing this with Robot Framework leaves automatic logs and is significantly easier than attempting to do the same with a Python or shell script.
In this guide we will go through some basic functionality of SeleniumLibrary that is used for web testing.

Background and installation#

Selenium is an independent testing tool for web automation and testing. This library just gives the same functionality, with the added benefit of much easier syntax and other Robot Framework functionalities, such as access to other libraries and the logging.

As stated in the overall guide, SeleniumLibrary is a third party library. Once Robot Framework itself has been installed, run pip install robotframework-seleniumlibrary

Usage#

To use the library add it under the Settings footer as part of the Library keyword.

***Settings***

Library     Seleniumlibrary

Full list of keyword can be found here

Typically you are going to open browser session, click some elements, possibly input some text, then check if a specific text or browser element appears or becomes absent as confirmation. And finally close the browser session.

Common keywords and locator strategy#

Opening browser session is the first necessary step. Typically you want this to be part of the setup phase for a test case or test suite and pair it with closing the browser on the teardown phase. Tests are ideally independent of each other, so you typically want to use fresh browser sessions on different unit tests, as previous tests on same session can possible affect new ones. Opening browser session looks like this

Open browser    <url to be opened>      <optional: browser>
Only the url parameter is mandatory, browser paramater determines browser to be used. By default it uses firefox. Closing the session is done with a different keyword.
Close Browser
This keyword has no parameters.

Moving on, the most typical part of automated task is clicking an element, such as confirmation button or symbolic link on a page. It is done with the following keyword

Click Element       <identifier>        <modifier, optional>
It also has another optional parameter, but it is out of scope for this guide. Modifier means combining mouseclick with simultaneous key press such as shift or ctrl. Identifier is a mandatory variable used to locate the web element. More on it later.

Next, we will look at inputting text

Input Text  <identifier>    <text to be input, can point to a file>
Simple syntax, there is also the Input Password that works identically but leaves scarcer logs for security purposes. Finally we will look at common keywords used to confirm a state after test has been executed.
Page Should Contain     <text to be searched>
Page Should Contain Element     <identifier>
Page Should Not Contain     <text to be searched>
Page Should Not Contain Element     <identifier>
The library has more specific keywords such Page Should Contain Image, List, Radio Button and so forth but they are mostly redundant as the same identifier is used to find the elements. In most tests you are attempting to manipulate the page to get it into some kind of state and these keywords are useful for confirming that something like a succesfull login prompt appears or is not appearing.

Identifier syntax and some strategies for finding locators#

Identifier is a mandatory variable that acts as unique locator for the browser element. In the most ideal scenario you can just use inspect element on a browser page, find the element you want to interact with and use the name or class of that element. But you will quite often find that something that looks like a button on a website is composed of multiple elements for the sake of visual appeal and the most apparent locator in fact does not work. Website elements can may also change their identifiers dynamically on each browser session. When writing a Seleniumlibrary script, it is not uncommon to spend more than half of your time finding locators that work. One helpful tool is the Selenium IDE browser addon that allows you to record website interaction, including the identifiers of clicked elements.

The identifier can use multitude of different strategies with very different syntax. The official guide is here, but we will cover some typical ones.
In the easiest cases the element has unique name or id that simply works. In this case the keyword would look something like the following

Click Element   id=272
Click Element   name=savebutton

But if simply providing an attribute like name, id or href does not work, you have to go deeper. One option is using xpath syntax, which stands for XML Path Language. For example, pressing an element on a site that has the text "Button" on it would look like this

 Click Element   xpath://*[contains(text(), "Save")]
The // refers to document root, * is a wildcard, in this case it refers to every element in the document under root. Inside the square brackets is the contains method, which specifies the given text "Save". This identifier works if the word is rendered as text and only appears on one element on the current site. For more details on xpath syntax refer to this guide

One other advanced identifier option is using css. It can perform similar things as xpath, but you might find the syntax more readable.

css:div#foo h1
This finds the header 1 element on a div with the id "foo". Useful for finding dynamically created elements that have randomized name or id. Locators can also be chained using a space, two greater than signs and another space like this
Click Image     id=foo >> href=bar 
In this case the keyword tries to find an image href "bar" within an element like a div with the id "foo". You can also mix other strategies like xpath and css with each other.