Using SeleniumLibrary and Python/Java keywords with same browser instance

Hi All,

I am trying to use Robot Framework’s -SeleniumLibrary, Custom Library in Python and Custom keyword in Java in the same test case.
I wanted to use the Robot Framework - SeleniumLibrary’s Open Browser keyword to open a browser instance and use the same browser which was opened from Robot Framework to be used by a Python / Java’s custom keyword. But a new browser is getting opened when I try to use these custom keywords.

Sample code of the Python’s Custom Keyword:

def login_using_selenium(self):  
        driver = webdriver.Chrome()
        element = driver.find_element_by_id("userId")
        element = driver.find_element_by_id("password")

Can anyone help me out in using the same browser in the Robot Framework test case along with RF-SeleniumLIbrary and other Custom Keywords?


Hi @Apoorva1 ,

i wouldn’t recommend to use it like this!
In your example you just use Selenium itself, without using the capabilities of SeleniumLibrary. Like automatic locator strategy etc.

What you should do:
Import the SeleniumLibrary FIRST in your robot tests and then import your library:

Library    SeleniumLibrary
Library    YourCustomLib

When you have done this, you have the possibility to get the SeleniumLibrary instance (the object of the library that also knows all open browsers etc) from RobotFramework
To do so, you call the BuiltIn keyword Get Library Instance from your library in Python.

from robot.libraries.BuiltIn import BuiltIn

SL = BuiltIn().get_library_instance("SeleniumLibrary")

class YourCustomLib:

    def login_user(self):
        SL.press_keys("userId", "33")
        SL.press_keys("password", "128")

Your Robot Test would look like

*** Settings ***
Library    SeleniumLibrary
Library    YourCustomLib

*** Test Cases ***
     Open Browser    browser=chrome
     Login User

You will not have code completion in Python while calling the keywords from SeleniumLibrary with the SL object. This could be fixed with a generated __init__.pyi file.
But this is another topic.

An alternative to SeleniumLibrary would be the new Browser library.

Hi @René

Thank you for your reply! I really appreciate it. Will try out the Browser library as well. But before that, I would like to know if there are any ways of using the browser instance vice versa.

I already have the Python/ Java code in place, which has it’s browser instance open. And then I would like to perform operations in the Robot Test Suite with the same browser instance.

*** Settings ***
Library SeleniumLibrary
Library MyCustomLib

*** Test Cases ***
Login User
Input Text id:HomePageInput_email

It’ll be really helpful if you could help us out on the same.

Apoorva A

Ifaik if you create a driver like in your example, it will be hard to habd over that driver to SeleniumLibrary. Maybe not impossible.

But i would check out public api of SeleniumLibrary.
Amd SeleniumLibrary 5.0 will also have code completion in python…