playwright-python icon indicating copy to clipboard operation
playwright-python copied to clipboard

Error: It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead.

Open SolarLee-83 opened this issue 5 years ago • 11 comments

System: window 10 , Python 3.7 playwright version1.8a01 After using "playwright codegen" to generate the code as shown below. And run the code in IDLE we found the Error: It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead.

from playwright.sync_api import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()

    # Open new page
    page = context.new_page()

    # Go to https://www.baidu.com/
    page.goto("https://www.baidu.com/")

    # Click input[name="wd"]
    page.click("input[name=\"wd\"]")

    # Fill input[name="wd"]
    page.fill("input[name=\"wd\"]", "京东")

    # Go to https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=京东&fenlei=256&rsv_pq=f6a37f6f0000194d&rsv_t=bc3fNWuNm6uvWag+TORZ+XH+NmCBPvd1ucStCeMV+0a7k0Njwn66xp1L8WA&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=13&rsv_sug1=13&rsv_sug7=100&rsv_btype=i&inputT=13279&rsv_sug4=17044
    page.goto("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=京东&fenlei=256&rsv_pq=f6a37f6f0000194d&rsv_t=bc3fNWuNm6uvWag+TORZ+XH+NmCBPvd1ucStCeMV+0a7k0Njwn66xp1L8WA&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=13&rsv_sug1=13&rsv_sug7=100&rsv_btype=i&inputT=13279&rsv_sug4=17044")

    # Close page
    page.close()

    # ---------------------
    context.close()
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

SolarLee-83 avatar Jan 27 '21 03:01 SolarLee-83

How do you run your python script?

pavelfeldman avatar Feb 02 '21 18:02 pavelfeldman

I am seeing the same issue with the following code while running the following code inside a Jupyter notebook:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto("http://www.google.com/")
    page.type("input[name=q]", "time")
    page.click("input[type=submit]")
    page.wait_for_load_state()
    page.screenshot(path=f'google.com.png')
    browser.close()

debakarr avatar Feb 18 '21 03:02 debakarr

You have to use async api of playwright to do so, please refer to #480

isara-ovin avatar Feb 26 '21 16:02 isara-ovin

Closing as per explanation above, please use async api w/ Jupyter.

pavelfeldman avatar Mar 02 '21 17:03 pavelfeldman

Actually there's a very easy fix for this issue. You're just missing some drivers, libraries and dependencies stuff that you can get if you run: playwright install-deps Then everything will work again without having to use Async Playwright

demelo-solutions avatar Nov 30 '23 12:11 demelo-solutions

Same issue It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead I only get this error when running in wsl and docker with more than 1 user. If I change from sync to async_playwright i go done the rabbit hole of asyncio vs gevent.

Is there an example of Locust/Playwright/Python/users > 1 in docker/wsl/linux (I have installed playwright install-debs) python3.12 locust2.24.1 playwright1.42.0 wsl 2.1.5.0 Ubuntu 20.04

KevinEvansSrAeries avatar Apr 27 '24 23:04 KevinEvansSrAeries

Actually there's a very easy fix for this issue. You're just missing some drivers, libraries and dependencies stuff that you can get if you run: playwright install-deps Then everything will work again without having to use Async Playwright

After building a separate Playwright image in Docker, I encountered the same problem when running it. Thank you for your suggestion, which helped me resolve the issue

cjydayang avatar Sep 18 '24 07:09 cjydayang

Hello @pavelfeldman. This should probably be reopened. As shown on #480, even using async functionality will trigger issues - at least on Windows - due to asyncio.

Copy-pasted from asheshstha back in 2022 (the issue persists):

Task exception was never retrieved
future: <Task finished name='Task-8' coro=<Connection.run() done, defined at C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\playwright\_impl\_connection.py:240> exception=NotImplementedError()>
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\playwright\_impl\_connection.py", line 247, in run
    await self._transport.connect()
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\playwright\_impl\_transport.py", line 132, in connect
    raise exc
  File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\playwright\_impl\_transport.py", line 120, in connect
    self._proc = await asyncio.create_subprocess_exec(
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\subprocess.py", line 218, in create_subprocess_exec
    transport, protocol = await loop.subprocess_exec(
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 1675, in subprocess_exec
    transport = await self._make_subprocess_transport(
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 498, in _make_subprocess_transport
    raise NotImplementedError
NotImplementedError

BramVanroy avatar Nov 13 '24 17:11 BramVanroy

I can confirm, on Win11 the issue is also there.

For the lines: async with async_playwright() as p: or p = await async_playwright().start() I get the error:

...\Python312\Lib\asyncio\base_events.py", line 523, in _make_subprocess_transport
    raise NotImplementedError
NotImplementedError

I am using it under Starlette.

Is there any fix for this?

stevstrong avatar Dec 05 '24 10:12 stevstrong

I saw the same error ("Error rendering page: It looks like you are using Playwright Sync API inside the asyncio loop.\nPlease use the Async API instead.") when calling the playwright sync api inside an asyncio loop but it looks like the page is still successfully rendered despite the error message. I am also using Starlette.

dinoDayo avatar Jan 27 '25 19:01 dinoDayo

i;m not using async at alll, but if you put code inside a generator, sync_playwright also bails (for no reason)

earonesty avatar May 22 '25 02:05 earonesty