Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

332
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda