Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

331
Views
Playwright use Page within the context of exposed function

I'm having trouble with Playwright's expose_function API. I want to be able to also pass Page into exposed functions to be able to navigate, use locators etc. withing the context of exposed function.

The problem is Page instance in exposed function becomes somehow "disconnected" from it's original, and doesn't do navigations, locators etc. Any calls to Page methods appear to block indefinitely.

Consider this simplified example:

from playwright.sync_api import sync_playwright, Page
from functools import partial


def search_title(page: Page, title: str):
    print(title)
    page.goto(f"https://bing.com?q={title}")


def main():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        context = browser.new_context(viewport={ 'width': 1920, 'height': 1024 })
        page = context.new_page()
        page.goto(f"https://wikipedia.org")
        context.expose_function("search_title", partial(handle_title, page))
        page.evaluate("window.onload = () => search_title(document.title)")
        while True:
            page.wait_for_timeout(1000)


if __name__ == "__main__":
    main()

What it does is prints the title, and halts at navigation step indefinitely.

EDIT: using asyncio version of the same code above produces playwright._impl._api_types.Error: Execution context was destroyed, most likely because of a navigation. error:



from playwright.async_api import  Page, async_playwright
import asyncio
from functools import partial
import time

async def handle_title(page: Page, title: str):
    print(title)
    print(page)
    await page.goto(f"https://bing.com?q={title}")


async def main():
     async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context(viewport={ 'width': 1920, 'height': 1024 })
        page = await context.new_page()
        await page.goto(f"https://wikipedia.org")
        await context.expose_function("handle_title", partial(handle_title, page))
        await page.evaluate("window.onload = () => handle_title(document.title)")
        while True:
            await asyncio.sleep(1)


if __name__ == "__main__":
    asyncio.run(main())
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I found that it's possible to use Page in context of exposed function, as long as:

  1. You use async Playwright (as per @hardkoded suggestion)
  2. Don't navigate away (as per @Charchit suggestion)
from playwright.async_api import  Page, async_playwright
import asyncio
from functools import partial
import time


async def handle_title(page: Page, title: str):
    # page = page_var.get()
    print(page)
    await page.locator("#searchInput").type(f"{title}")
    return "hello world"


async def main():
     async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context(viewport={ 'width': 1920, 'height': 1024 })
        page = await context.new_page()
        await page.goto(f"https://wikipedia.org")
        await context.expose_function("handle_title", partial(handle_title, page))
        await page.evaluate("window.onload = () => handle_title(document.title)")
        while True:
            await asyncio.sleep(1)



asyncio.run(main())
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!