# Maintain Browser Session from Custom Library

**URL:** <https://forum.robotframework.org/t/maintain-browser-session-from-custom-library/8163>\
**Category:** Browser\
**Created:** [12 December 2024 22:41 UTC](https://forum.robotframework.org/t/maintain-browser-session-from-custom-library/8163 "2024-12-12T22:41:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbfw](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@rbfw](https://forum.robotframework.org/u/rbfw)\
**Post date:** [12 December 2024 22:41 UTC](https://forum.robotframework.org/t/maintain-browser-session-from-custom-library/8163/1 "2024-12-12T22:41:00Z")

</div>

Problem: How do you maintain the session and pass it back to be used in a following Keyword?  
Error: Could not find active page

my robot file has a test:  
authentication a user this is via a custom library which works by calling browser() in the function via from Browser import Browser  
Click “Go To This Link” this would be using Library Browser in the robot file

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [13 December 2024 01:12 UTC](https://forum.robotframework.org/t/maintain-browser-session-from-custom-library/8163/2 "2024-12-13T01:12:53Z")

</div>

Hi Bob,

Your question is not clear what you are after, but I think you need to establish and maintain a context? See [Browser, Context and Page](https://marketsquare.github.io/robotframework-browser/Browser.html#Browser%2C%20Context%20and%20Page) for details.

hope that helps,

Dave.

---

<div class="post-metadata">

**Author:** ![rbfw](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@rbfw](https://forum.robotframework.org/u/rbfw)\
**Post date:** [13 December 2024 16:18 UTC](https://forum.robotframework.org/t/maintain-browser-session-from-custom-library/8163/3 "2024-12-13T16:18:25Z")

</div>

It appears as though the browser object when defined in the custom library doesn’t pass back to the calling robot file so that it can be used by Library Browser in the Click Click “Keyword Documentation” step.

ERROR: Could not find active page

ROBO.ROBOT  
\*\*\* Settings \*\*\*  
Library Browser  
Library robo\_lib.py

\*\*\* Test Cases \*\*\*  
Test Using Python File  
Test Keyword Filtering  
Click “Keyword Documentation”  
[Teardown] Close Browser

ROBO\_LIB.PY  
from Browser import Browser  
from Browser import SupportedBrowsers

browser = Browser()

def test\_keyword\_filtering():  
browser.new\_browser(SupportedBrowsers.chromium, headless=False)  
browser.new\_page(“[https://robotframework-browser.org/](https://robotframework-browser.org/)”)  
assert browser.get\_title() == “Browser Library”

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [13 December 2024 23:23 UTC](https://forum.robotframework.org/t/maintain-browser-session-from-custom-library/8163/4 "2024-12-13T23:23:37Z")

</div>

Hi Bob,

I’ll have to leave that for a Browser Library maintainer to answer, but it looks to me like you have 2 instances of browser library, one in robot framework, and a separate one in the python file, so I’m not surprised by the error you got, but I also don’t know how to make your test case work the way you want.

Dave.

---

<div class="post-metadata">

**Author:** ![rbfw](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@rbfw](https://forum.robotframework.org/u/rbfw)\
**Post date:** [16 December 2024 20:25 UTC](https://forum.robotframework.org/t/maintain-browser-session-from-custom-library/8163/5 "2024-12-16T20:25:37Z")

</div>

Seems as though this is the answer. Start the initial browser process off in the robot file. grab the started library Browser reference using BuiltIn().get\_library\_instance(‘Browser’) in the custom python library (do what you need to do there…), then you’ll be able to continue on the robot file with additional browser steps. Only Caveat seems like code complete doesn’t work on the builtIn object (at least in Pycharm for me)

\*\*\* Settings \*\*\*  
Library Browser  
Library RoboLib.py

\*\*\* Test Cases \*\*\*  
Test Using Python File  
New Page [https://www.google.com](https://www.google.com)  
Test Custom Lib Keyword  
Keyboard Key PRESS Enter  
[Teardown] Close Browser

from Browser.utils.data\_types import ElementRole  
from robot.api.logger import info  
from robot.libraries.BuiltIn import BuiltIn

class RoboLib:

```
ROBOT_LIBRARY_SCOPE = 'SUITE'

def test_custom_lib_keyword(self):

    pw_browser = BuiltIn().get_library_instance('Browser')

    info(pw_browser.get_title())
    assert pw_browser.get_title() == "Google"
    pw_browser.type_text(pw_browser.get_element_by_role(ElementRole.COMBOBOX, name="Search"), "Puppies")

```
