Use Playwright browser opened in my library

Hello,

I’m wondering how to use a browser window - opened from within my library - in my project’s code.

I created a library that logs in and out of a website. I imported that library into my project and can run those two keywords without issues. Now, in between those two keywoards, I want to continue developing the project, using that already opened window. However, the lines in between the log in and out complain that Error: Could not find active page. I’ve tried reading the browser catalog and browser id in order to switch / attach to it, but both appear as empty lists. I’ve also modified my Log In to return the opened browser, but no luck. It’s like my project has no knowledge or visibility of the browser / context from my library.

MyLib.py:

from RPA.Browser.Playwright import Playwright

class MyLib:
    PW = Playwright()

    def log_in(self, username, password):
        MyLib.PW.new_browser(headless=False)
        MyLib.PW.new_page(<url>)

        MyLib.PW.type_text("id=login_id", username)
        MyLib.PW.press_keys("id=login_id", "Enter")
        MyLib.type_text("id=password", password)
        MyLib.PW.press_keys("id=password", "Enter")

        return MyLib.PW

    def log_out(self):
        MyLib.PW.click("id=sign_out")

My project:

*** Settings ***
Documentation       Template robot main suite.

Library             RPA.Browser.Playwright
Library             MyLib


*** Tasks ***
Log In Click and Log Out
    ${browser}=    Log In    <username>    <password>     # does not appear to return the browser
    ${browser_catalog}=    Get Browser Catalog            # empty list []
    ${browser_ids}=    Get Browser Ids                    # empty list []
    Click    id=some_id              # Error: Could not find active page
    Log Out

Is MyLib structured correctly? Is the issue in my project?

Thanks

Hi @jaime.salazar !

In short the problem is that you have 2 active instances of the Playwright library in your code.

One instance is created in the .robot file and another in your Python code. This means that both instances have their own “view” of the Playwright browser instances and their state.

Here is one way of sharing the Browser instance between Python and Robot Framework

MyLib.py

from robot.libraries.BuiltIn import BuiltIn

class MyLib:
    def __init__(self) -> None:
        self.PW = None
    
    def init_mylib_library(self):
        self.PW = BuiltIn().get_library_instance("RPA.Browser.Playwright")
        self.PW.new_browser(headless=False)
        self.PW.new_page("about:blank")

    def log_in(self, username, password):
        self.PW.go_to("https://cloud.robocorp.com")
        #self.PW.type_text("id=login_id", username)
        #self.PW.press_keys("id=login_id", "Enter")
        #self.type_text("id=password", password)
        #self.PW.press_keys("id=password", "Enter")

    def log_out(self):
        self.PW.click("id=sign_out")

and the tasks.robot

*** Settings ***
Library   RPA.Browser.Playwright
Library   MyLib


*** Tasks ***
Minimal task
    Init MyLib Library
    Sleep  10s
    Go To   https://docs.robocorp.com
    Log In  user   pass
1 Like

This works great. Thanks a lot!

1 Like