Question: Context After initialisation of a playwright browser object in function initialise_playwright_browsercontroller, I try to use its Page object in another function. However, that yields get error:  "Event loop is closed! Is Playwright already stopped?"  If I use the Page object in the same function (and with statement) in which it is created, the error does not occur. MWE Below is the an MWE that demonstrates the error: from playwright.sync_api import sync_playwright from playwright.sync_api._generated import Locator # type: ignore[import] from playwright.sync_api._generated import Browser, Page from typeguard import typechecked @typechecked def initialise_playwright_browsercontroller( *, start_url: str, ) -> tuple[Browser, Page]: """Creates a Playwright browser, opens a new page, and navigates to a specified URL. Returns: tuple[Browser, Page]: A tuple containing the browser and page objects. """ with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: if ( browser_type.name != "webkit" and browser_type.name == "firefox" ): browser = browser_type.launch() # Create a new page and navigate to the URL page = browser.new_page() page.goto(start_url) # Return the browser and page objects return browser, page raise ValueError("Error: Could not find browser.") @typechecked def do_something_on_webpage() -> None: """Tries to do something with the object received from the the browser.""" # Declare the browser and page objects. browser: Browser page: Page # Specify the website to go to. start_url: str = "https://github.com" browser, page = initialise_playwright_browsercontroller( start_url=start_url ) print(f"page url = {page.url}") sign_in_button: Locator = page.locator("Sign in") sign_in_button.click() print("Done.") do_something_on_webpage()  Question How can I create the Page object elsewhere, and pass it around and use it in other functions?

Answer: If you want to use context manager, then you should use yield instead of return from playwright.sync_api import sync_playwright from playwright.sync_api._generated import Locator # type: ignore[import] from playwright.sync_api._generated import Browser, Page from typeguard import typechecked import contextlib @typechecked @contextlib.contextmanager def initialise_playwright_browsercontroller( *, start_url: str, ) -> tuple[Browser, Page]: """Creates a Playwright browser, opens a new page, and navigates to a specified URL. Returns: tuple[Browser, Page]: A tuple containing the browser and page objects. """ with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: if ( browser_type.name != "webkit" and browser_type.name == "firefox" ): browser = browser_type.launch() # Create a new page and navigate to the URL page = browser.new_page() page.goto(start_url) # Return the browser and page objects yield browser, page return @typechecked def do_something_on_webpage() -> None: """Tries to do something with the object received from the the browser.""" # Specify the website to go to. start_url: str = "https://github.com" with initialise_playwright_browsercontroller( start_url=start_url ) as (browser, page): print(f"page url = {page.url}") sign_in_button: Locator = page.locator("Sign in") sign_in_button.click() print("Done.") do_something_on_webpage()