Robot Framework SeleniumLibrary
SeleniumLibrary.SeleniumLibrary Class Reference

SeleniumLibrary is a web testing library for Robot Framework. More...

Inheritance diagram for SeleniumLibrary.SeleniumLibrary:

Public Member Functions

def __init__ (self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None, plugins=None, event_firing_webdriver=None)
 SeleniumLibrary can be imported with several optional arguments. More...
 
def failure_occurred (self)
 Method that is executed when a SeleniumLibrary keyword fails. More...
 
def find_element (self, locator, parent=None)
 Find element matching locator. More...
 
def find_elements (self, locator, parent=None)
 Find all elements matching locator. More...
 
def get_keyword_tags (self, name)
 
def register_driver (self, driver, alias)
 Add's a driver to the library WebDriverCache. More...
 
def run_keyword (self, name, args, kwargs)
 

Public Attributes

 event_firing_webdriver
 
 implicit_wait
 
 ROBOT_LIBRARY_LISTENER
 
 screenshot_root_directory
 
 speed
 
 timeout
 

Static Public Attributes

string ROBOT_LIBRARY_SCOPE = 'GLOBAL'
 
 ROBOT_LIBRARY_VERSION = __version__
 

Properties

 driver = property
 Current active driver. More...
 

Private Member Functions

def _parse_listener (self, event_firing_webdriver)
 
def _parse_plugins (self, plugins)
 
def _store_plugin_keywords (self, plugin)
 
def _string_to_modules (self, modules)
 
def driver (self)
 

Private Attributes

 _drivers
 
 _element_finder
 
 _plugin_keywords
 
 _running_on_failure_keyword
 

Detailed Description

SeleniumLibrary is a web testing library for Robot Framework.

This document explains how to use keywords provided by SeleniumLibrary.
For information about installation, support, and more, please visit the
[https://github.com/robotframework/SeleniumLibrary|project pages].
For more information about Robot Framework, see http://robotframework.org.

SeleniumLibrary uses the Selenium WebDriver modules internally to
control a web browser. See http://seleniumhq.org for more information
about Selenium in general and SeleniumLibrary README.rst
[https://github.com/robotframework/SeleniumLibrary#browser-drivers|Browser drivers chapter]
for more details about WebDriver binary installation.

== Table of contents ==

- `Locating elements`
- `Timeouts, waits and delays`
- `Run-on-failure functionality`
- `Boolean arguments`
- `Plugins`
- `EventFiringWebDriver`
- `Thread support`
- `Importing`
- `Shortcuts`
- `Keywords`

= Locating elements =

All keywords in SeleniumLibrary that need to interact with an element
on a web page take an argument typically named ``locator`` that specifies
how to find the element. Most often the locator is given as a string
using the locator syntax described below, but `using WebElements` is
possible too.

== Locator syntax ==

SeleniumLibrary supports finding elements based on different strategies
such as the element id, XPath expressions, or CSS selectors. The strategy
can either be explicitly specified with a prefix or the strategy can be
implicit.

=== Default locator strategy ===

By default locators are considered to use the keyword specific default
locator strategy. All keywords support finding elements based on ``id``
and ``name`` attributes, but some keywords support additional attributes
or other values that make sense in their context. For example, `Click
Link` supports the ``href`` attribute and the link text and addition
to the normal ``id`` and ``name``.

Examples:

| `Click Element` | example | # Match based on ``id`` or ``name``.            |
| `Click Link`    | example | # Match also based on link text and ``href``.   |
| `Click Button`  | example | # Match based on ``id``, ``name`` or ``value``. |

If a locator accidentally starts with a prefix recognized as `explicit
locator strategy` or `implicit XPath strategy`, it is possible to use
the explicit ``default`` prefix to enable the default strategy.

Examples:

| `Click Element` | name:foo         | # Find element with name ``foo``.               |
| `Click Element` | default:name:foo | # Use default strategy with value ``name:foo``. |
| `Click Element` | //foo            | # Find element using XPath ``//foo``.           |
| `Click Element` | default: //foo   | # Use default strategy with value ``//foo``.    |

=== Explicit locator strategy ===

The explicit locator strategy is specified with a prefix using either
syntax ``strategy:value`` or ``strategy=value``. The former syntax
is preferred, because the latter is identical to Robot Framework's
[http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#named-argument-syntax|
named argument syntax] and that can cause problems. Spaces around
the separator are ignored, so ``id:foo``, ``id: foo`` and ``id : foo``
are all equivalent.

Locator strategies that are supported by default are listed in the table
below. In addition to them, it is possible to register `custom locators`.

| = Strategy = |          = Match based on =         |         = Example =            |
| id           | Element ``id``.                     | ``id:example``                 |
| name         | ``name`` attribute.                 | ``name:example``               |
| identifier   | Either ``id`` or ``name``.          | ``identifier:example``         |
| class        | Element ``class``.                  | ``class:example``              |
| tag          | Tag name.                           | ``tag:div``                    |
| xpath        | XPath expression.                   | ``xpath://div[@id="example"]`` |
| css          | CSS selector.                       | ``css:div#example``            |
| dom          | DOM expression.                     | ``dom:document.images[5]``     |
| link         | Exact text a link has.              | ``link:The example``           |
| partial link | Partial link text.                  | ``partial link:he ex``         |
| sizzle       | Sizzle selector deprecated.         | ``sizzle:div.example``         |
| jquery       | jQuery expression.                  | ``jquery:div.example``         |
| default      | Keyword specific default behavior.  | ``default:example``            |

See the `Default locator strategy` section below for more information
about how the default strategy works. Using the explicit ``default``
prefix is only necessary if the locator value itself accidentally
matches some of the explicit strategies.

Different locator strategies have different pros and cons. Using ids,
either explicitly like ``id:foo`` or by using the `default locator
strategy` simply like ``foo``, is recommended when possible, because
the syntax is simple and locating elements by an id is fast for browsers.
If an element does not have an id or the id is not stable, other
solutions need to be used. If an element has a unique tag name or class,
using ``tag``, ``class`` or ``css`` strategy like ``tag:h1``,
``class:example`` or ``css:h1.example`` is often an easy solution. In
more complex cases using XPath expressions is typically the best
approach. They are very powerful but a downside is that they can also
get complex.

Examples:

| `Click Element` | id:foo                      | # Element with id 'foo'. |
| `Click Element` | css:div#foo h1              | # h1 element under div with id 'foo'. |
| `Click Element` | xpath: //div[@id="foo"]//h1 | # Same as the above using XPath, not CSS. |
| `Click Element` | xpath: //*[contains(text(), "example")] | # Element containing text 'example'. |

*NOTE:*

- The ``strategy:value`` syntax is only supported by SeleniumLibrary 3.0
  and newer.
- Using the ``sizzle`` strategy or its alias ``jquery`` requires that
  the system under test contains the jQuery library.
- Prior to SeleniumLibrary 3.0, table related keywords only supported
  ``xpath``, ``css`` and ``sizzle/jquery`` strategies.

=== Implicit XPath strategy ===

If the locator starts with ``//`` or ``(//``, the locator is considered
to be an XPath expression. In other words, using ``//div`` is equivalent
to using explicit ``xpath://div``.

Examples:

| `Click Element` | //div[@id="foo"]//h1 |
| `Click Element` | (//div)[2]           |

The support for the ``(//`` prefix is new in SeleniumLibrary 3.0.

== Using WebElements ==

In addition to specifying a locator as a string, it is possible to use
Selenium's WebElement objects. This requires first getting a WebElement,
for example, by using the `Get WebElement` keyword.

| ${elem} =       | `Get WebElement` | id:example |
| `Click Element` | ${elem}          |            |

== Custom locators ==

If more complex lookups are required than what is provided through the
default locators, custom lookup strategies can be created. Using custom
locators is a two part process. First, create a keyword that returns
a WebElement that should be acted on:

| Custom Locator Strategy | [Arguments] | ${browser} | ${locator} | ${tag} | ${constraints} |
|   | ${element}= | Execute Javascript | return window.document.getElementById('${locator}'); |
|   | [Return] | ${element} |

This keyword is a reimplementation of the basic functionality of the
``id`` locator where ``${browser}`` is a reference to a WebDriver
instance and ``${locator}`` is name of the locator strategy. To use
this locator it must first be registered by using the
`Add Location Strategy` keyword:

| `Add Location Strategy` | custom | Custom Locator Strategy |

The first argument of `Add Location Strategy` specifies the name of
the strategy and it must be unique. After registering the strategy,
the usage is the same as with other locators:

| `Click Element` | custom:example |

See the `Add Location Strategy` keyword for more details.

= Timeouts, waits and delays =

This section discusses different ways how to wait for elements to
appear on web pages and to slow down execution speed otherwise.
It also explains the `time format` that can be used when setting various
timeouts, waits and delays.

== Timeout ==

SeleniumLibrary contains various keywords that have an optional
``timeout`` argument that specifies how long these keywords should
wait for certain events or actions. These keywords include, for example,
``Wait ...`` keywords and keywords related to alerts. Additionally
`Execute Async Javascript`. although it does not have ``timeout``,
argument, uses timeout to define how long asynchronous JavaScript
can run.

The default timeout these keywords use can be set globally either by
using the `Set Selenium Timeout` keyword or with the ``timeout`` argument
when `importing` the library. See `time format` below for supported
timeout syntax.

== Implicit wait ==

Implicit wait specifies the maximum time how long Selenium waits when
searching for elements. It can be set by using the `Set Selenium Implicit
Wait` keyword or with the ``implicit_wait`` argument when `importing`
the library. See [https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp|
Selenium documentation] for more information about this functionality.

See `time format` below for supported syntax.

== Selenium speed ==

Selenium execution speed can be slowed down globally by using `Set
Selenium speed` keyword. This functionality is designed to be used for
demonstrating or debugging purposes. Using it to make sure that elements
appear on a page is not a good idea, and the above explained timeouts
and waits should be used instead.

See `time format` below for supported syntax.

== Time format ==

All timeouts and waits can be given as numbers considered seconds
(e.g. ``0.5`` or ``42``) or in Robot Framework's time syntax
(e.g. ``1.5 seconds`` or ``1 min 30 s``). For more information about
the time syntax see the
[http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#time-format|Robot Framework User Guide].

= Run-on-failure functionality =

SeleniumLibrary has a handy feature that it can automatically execute
a keyword if any of its own keywords fails. By default it uses the
`Capture Page Screenshot` keyword, but this can be changed either by
using the `Register Keyword To Run On Failure` keyword or with the
``run_on_failure`` argument when `importing` the library. It is
possible to use any keyword from any imported library or resource file.

The run-on-failure functionality can be disabled by using a special
value ``NOTHING`` or anything considered false (see `Boolean arguments`)
such as ``NONE``.

= Boolean arguments =

Some keywords accept arguments that are handled as Boolean values true or
false. If such an argument is given as a string, it is considered false if
it is either empty or case-insensitively equal to ``false``, ``no``, ``off``,
 ``0`` or ``none``. Other strings are considered true regardless their value, and
other argument types are tested using same
[https://docs.python.org/3/library/stdtypes.html#truth-value-testing|rules as in Python].

True examples:

| `Set Screenshot Directory` | ${RESULTS} | persist=True    | # Strings are generally true.    |
| `Set Screenshot Directory` | ${RESULTS} | persist=yes     | # Same as the above.             |
| `Set Screenshot Directory` | ${RESULTS} | persist=${TRUE} | # Python True is true.           |
| `Set Screenshot Directory` | ${RESULTS} | persist=${42}   | # Numbers other than 0 are true. |

False examples:

| `Set Screenshot Directory` | ${RESULTS} | persist=False    | # String false is false.        |
| `Set Screenshot Directory` | ${RESULTS} | persist=no       | # Also string no is false.      |
| `Set Screenshot Directory` | ${RESULTS} | persist=NONE     | # String NONE is false.         |
| `Set Screenshot Directory` | ${RESULTS} | persist=${EMPTY} | # Empty string is false.        |
| `Set Screenshot Directory` | ${RESULTS} | persist=${FALSE} | # Python False is false.        |
| `Set Screenshot Directory` | ${RESULTS} | persist=${NONE}  | # Python None is false.         |

Note that prior to SeleniumLibrary 3.0, all non-empty strings, including
``false``, ``no`` and ``none``, were considered true. Starting from
SeleniumLibrary 4.0, strings ``0`` and ``off`` are considered as false.

= Plugins =

SeleniumLibrary offers plugins as a way to modify and add library keywords and modify some of the internal
functionality without creating new library or hacking the source code. See
[https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending.rst#Plugins|plugin API]
documentation for further details.

Plugin API is new SeleniumLibrary 4.0

= EventFiringWebDriver =

The SeleniumLibrary offers support for
[https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.event_firing_webdriver.html#module-selenium.webdriver.support.event_firing_webdriver|EventFiringWebDriver].
See the Selenium and SeleniumLibrary
[https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending.rst#EventFiringWebDriver|EventFiringWebDriver support]
documentation for futher details.

EventFiringWebDriver is new in SeleniumLibrary 4.0

= Thread support =

SeleniumLibrary is not thread safe. This is mainly due because the underlying
[https://github.com/SeleniumHQ/selenium/wiki/Frequently-Asked-Questions#q-is-webdriver-thread-safe|
Selenium tool is not thread safe] within one browser/driver instance.
Because of the limitation in the Selenium side, the keywords or the
API provided by the SeleniumLibrary is not thread safe.

Definition at line 344 of file __init__.py.

Constructor & Destructor Documentation

◆ __init__()

def SeleniumLibrary.SeleniumLibrary.__init__ (   self,
  timeout = 5.0,
  implicit_wait = 0.0,
  run_on_failure = 'Capture Page Screenshot',
  screenshot_root_directory = None,
  plugins = None,
  event_firing_webdriver = None 
)

SeleniumLibrary can be imported with several optional arguments.

    - ``timeout``:
      Default value for `timeouts` used with ``Wait ...`` keywords.
    - ``implicit_wait``:
      Default value for `implicit wait` used when locating elements.
    - ``run_on_failure``:
      Default action for the `run-on-failure functionality`.
    - ``screenshot_root_directory``:
      Location where possible screenshots are created. If not given,
      the directory where the log file is written is used.
    - ``plugins``:
      Allows extending the SeleniumLibrary with external Python classes.
    - ``event_firing_webdriver``:
      Class for wrapping Selenium with
      [https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.event_firing_webdriver.html#module-selenium.webdriver.support.event_firing_webdriver|EventFiringWebDriver]

Definition at line 365 of file __init__.py.

Member Function Documentation

◆ _parse_listener()

def SeleniumLibrary.SeleniumLibrary._parse_listener (   self,
  event_firing_webdriver 
)
private

Definition at line 507 of file __init__.py.

◆ _parse_plugins()

def SeleniumLibrary.SeleniumLibrary._parse_plugins (   self,
  plugins 
)
private

Definition at line 490 of file __init__.py.

◆ _store_plugin_keywords()

def SeleniumLibrary.SeleniumLibrary._store_plugin_keywords (   self,
  plugin 
)
private

Definition at line 540 of file __init__.py.

◆ _string_to_modules()

def SeleniumLibrary.SeleniumLibrary._string_to_modules (   self,
  modules 
)
private

Definition at line 521 of file __init__.py.

◆ driver()

def SeleniumLibrary.SeleniumLibrary.driver (   self)
private

Definition at line 455 of file __init__.py.

◆ failure_occurred()

def SeleniumLibrary.SeleniumLibrary.failure_occurred (   self)

Method that is executed when a SeleniumLibrary keyword fails.

    By default executes the registered run-on-failure keyword.
    Libraries extending SeleniumLibrary can overwrite this hook
    method if they want to provide custom functionality instead.

Definition at line 435 of file __init__.py.

◆ find_element()

def SeleniumLibrary.SeleniumLibrary.find_element (   self,
  locator,
  parent = None 
)

Find element matching locator.

    :param locator: Locator to use when searching the element.
        See library documentation for the supported locator syntax.
    :type locator: str or selenium.webdriver.remote.webelement.WebElement
    :param parent: Optional parent `WebElememt` to search child elements
        from. By default search starts from the root using `WebDriver`.
    :type parent: selenium.webdriver.remote.webelement.WebElement
    :return: Found `WebElement`.
    :rtype: selenium.webdriver.remote.webelement.WebElement
    :raises SeleniumLibrary.errors.ElementNotFound: If element not found.

Definition at line 472 of file __init__.py.

◆ find_elements()

def SeleniumLibrary.SeleniumLibrary.find_elements (   self,
  locator,
  parent = None 
)

Find all elements matching locator.

    :param locator: Locator to use when searching the element.
        See library documentation for the supported locator syntax.
    :type locator: str or selenium.webdriver.remote.webelement.WebElement
    :param parent: Optional parent `WebElememt` to search child elements
        from. By default search starts from the root using `WebDriver`.
    :type parent: selenium.webdriver.remote.webelement.WebElement
    :return: list of found `WebElement` or e,mpty if elements are not found.
    :rtype: list[selenium.webdriver.remote.webelement.WebElement]

Definition at line 486 of file __init__.py.

◆ get_keyword_tags()

def SeleniumLibrary.SeleniumLibrary.get_keyword_tags (   self,
  name 
)

Definition at line 411 of file __init__.py.

◆ register_driver()

def SeleniumLibrary.SeleniumLibrary.register_driver (   self,
  driver,
  alias 
)

Add's a driver to the library WebDriverCache.

    :param driver: Instance of the Selenium `WebDriver`.
    :type driver: selenium.webdriver.remote.webdriver.WebDriver
    :param alias: Alias given for this `WebDriver` instance.
    :type alias: str
    :return: The index of the `WebDriver` instance.
    :rtype: int

Definition at line 426 of file __init__.py.

◆ run_keyword()

def SeleniumLibrary.SeleniumLibrary.run_keyword (   self,
  name,
  args,
  kwargs 
)

Definition at line 404 of file __init__.py.

Member Data Documentation

◆ _drivers

SeleniumLibrary.SeleniumLibrary._drivers
private

Definition at line 396 of file __init__.py.

◆ _element_finder

SeleniumLibrary.SeleniumLibrary._element_finder
private

Definition at line 376 of file __init__.py.

◆ _plugin_keywords

SeleniumLibrary.SeleniumLibrary._plugin_keywords
private

Definition at line 377 of file __init__.py.

◆ _running_on_failure_keyword

SeleniumLibrary.SeleniumLibrary._running_on_failure_keyword
private

Definition at line 374 of file __init__.py.

◆ event_firing_webdriver

SeleniumLibrary.SeleniumLibrary.event_firing_webdriver

Definition at line 400 of file __init__.py.

◆ implicit_wait

SeleniumLibrary.SeleniumLibrary.implicit_wait

Definition at line 370 of file __init__.py.

◆ ROBOT_LIBRARY_LISTENER

SeleniumLibrary.SeleniumLibrary.ROBOT_LIBRARY_LISTENER

Definition at line 398 of file __init__.py.

◆ ROBOT_LIBRARY_SCOPE

string SeleniumLibrary.SeleniumLibrary.ROBOT_LIBRARY_SCOPE = 'GLOBAL'
static

Definition at line 345 of file __init__.py.

◆ ROBOT_LIBRARY_VERSION

SeleniumLibrary.SeleniumLibrary.ROBOT_LIBRARY_VERSION = __version__
static

Definition at line 346 of file __init__.py.

◆ screenshot_root_directory

SeleniumLibrary.SeleniumLibrary.screenshot_root_directory

Definition at line 375 of file __init__.py.

◆ speed

SeleniumLibrary.SeleniumLibrary.speed

Definition at line 371 of file __init__.py.

◆ timeout

SeleniumLibrary.SeleniumLibrary.timeout

Definition at line 369 of file __init__.py.

Property Documentation

◆ driver

SeleniumLibrary.SeleniumLibrary.driver = property
static

Current active driver.

    :rtype: selenium.webdriver.remote.webdriver.WebDriver
    :raises SeleniumLibrary.errors.NoOpenBrowser: If browser is not open.

Definition at line 453 of file __init__.py.


The documentation for this class was generated from the following file: