简体   繁体   中英

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())

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM