# Pass the Browser Instance to CustomLibrary

**URL:** <https://forum.robotframework.org/t/pass-the-browser-instance-to-customlibrary/5974>\
**Category:** SeleniumLibrary\
**Created:** [21 July 2023 09:40 UTC](https://forum.robotframework.org/t/pass-the-browser-instance-to-customlibrary/5974 "2023-07-21T09:40:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TobiasM](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/tobiasm/32/3477_2.png) [@TobiasM](https://forum.robotframework.org/u/TobiasM)\
**Post date:** [21 July 2023 09:40 UTC](https://forum.robotframework.org/t/pass-the-browser-instance-to-customlibrary/5974/1 "2023-07-21T09:40:06Z")

</div>

Hello everybody,

I’m new to Robot Framework and Keyword based programming.

I got the Following Code files:

Test.robot

> **Summary**
>
> \*\*\* Settings \*\*\*
> 
> Documentation RobotFramework-testen  
> Metadata Author Tobias Marx  
> Metadata Version 1.1  
> Force Tags IPRL-Testing  
> Suite Setup Open Browser \ ${BROWSER}  
> Suite Teardown Close Browser  
> Library SeleniumLibrary  
> Library OperatingSystem  
> Resource …/Resources/locators.resource  
> Resource …/Resources/methods.resource
> 
> \*\*\* Keywords \*\*\*
> 
> \*\*\* Variables \*\*\*  
> ${Umgebung} \*\*\*\*\*  
> ${URL} \*\*\*\*\*  
> ${BROWSER} chrome  
> ${USERNAME} \*\*\*\*\*  
> ${PASSWORD} \*\*\*\*\*  
> ${OUTPUT\_DIR} /log/
> 
> \*\*\* Test Cases \*\*\*  
> Test  
> Navigate and Login ${URL} ${USERNAME\_TEXTAREA} ${USERNAME} ${PASSWORD\_TEXTAREA} ${PASSWORD} ${ANMELDEN\_BUTTON}
> 
> Einfach Weiter  
> Click Element ${AKTUELLE\_AUSSCHR\_LINK}  
> Sleep 1  
> Click Element ${AUSSCHR\_VERW\_LINK}  
> Sleep 1  
> Click Element ${PRODUKTART\_DROPDOWN}  
> Sleep 1  
> Click Element ${PRODUKTART\_DD\_SRL}  
> Sleep 2  
> Search Open Run  
> Sleep 5

Methods.resource

> **Summary**
>
> \*\*\* Settings \*\*\*  
> Library OperatingSystem  
> Library SeleniumLibrary  
> Library Collections  
> Library CustomLibrary.py
> 
> \*\*\* Keywords \*\*\*
> 
> Search Open Run  
> Find And Click Link

CustomLibrary.py

> **Summary**
>
> from SeleniumLibrary import SeleniumLibrary  
> from SeleniumLibrary.base import keyword, LibraryComponent  
> from SeleniumLibrary.keywords import BrowserManagementKeywords  
> from robot.libraries.BuiltIn import BuiltIn  
> from selenium.webdriver.common.by import By
> 
> def find\_and\_click\_link():  
> flag = False  
> link = None  
> seleniumlib = BuiltIn().get\_library\_instance(“SeleniumLibrary”)
> 
> ```
> # Find the table
> table = seleniumlib.find_element(By.TAG_NAME, "table")
> 
> # Get all rows in the table
> rows = table.find_elements(By.TAG_NAME, "tr")
> 
> for row in rows:
> # Get the cells in each row
> cells = row.find_elements(By.TAG_NAME, "td")
> 
> # Iterate over the cells
> for cell in cells:
> if flag:
> cell.find_element(By.TAG_NAME, "a").click()
> break
> elif cell.text == "Warte auf Vergabevorschlag":
> flag = True
> 
> if flag:
> flag = False
> break
> 
> ```

My Problem is, i need to pass the browser Instance i started in the Test.robot file to my CustomLibrary.py  
But i always get errors like this:  
ValueError: Parent must be Selenium WebElement but it was \<class ‘str’\>.

---

<div class="post-metadata">

**Author:** ![daluu](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/daluu/32/495_2.png) [@daluu](https://forum.robotframework.org/u/daluu)\
**Post date:** [1 August 2023 21:50 UTC](https://forum.robotframework.org/t/pass-the-browser-instance-to-customlibrary/5974/2 "2023-08-01T21:50:06Z")

</div>

I don’t think that is possible:

Passing a custom object between Robot files or between a Robot file and custom library code (in python, Java, remote library, etc.). Such functionality and support I believe is not defined nor implemented in the Robot Framework infrastructure.

Instead, you may have to workaround the limitation and wrap the generic selenium library code in your robot to be within custom library and then just call that from robot file. So thus your custom library contains some more generic boilerplate setup/teardown functions.

And if you ever have to pass a reference of the browser (or rather the WebDriver) between robot file and code library, then your best option is to handle the logic of dealing with reference in the code library, and you may have to use one library, can’t easily mix two different libraries to share reference. And how you pass the reference in/out to robot file could be a unique identifier string on robot file side, on the custom code library side, you maintain a dict/map of unique ID strings to actual object references. So with this approach, you aren’t actually passing the reference or instance around, you’re just passing the ID link to the instance/reference held within the custom library itself. Such only works with custom libraries as well - I don’t think such a feature is built into the SeleniumLibrary itself but I could be wrong.
