microsoft / playwright-python

Python version of the Playwright testing and automation library.
https://playwright.dev/python/
Apache License 2.0
11.89k stars 906 forks source link

Exception: Connection closed while reading from the driver #2530

Closed xjtupy closed 2 months ago

xjtupy commented 2 months ago

Hi, Try running the following code in Jupyter notebook

import asyncio
import nest_asyncio
nest_asyncio.apply()

from playwright.async_api import async_playwright

url = "https://blog.csdn.net/mopmgerg54mo/article/details/141028116"
async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        await page.goto(url, wait_until="domcontentloaded")
        await browser.close()
        print(page)

asyncio.run(main())

Report an error:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
[/tmp/ipykernel_188540/1335809689.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/tmp/ipykernel_188540/1335809689.py) in <module>
     14         print(page)
     15 
---> 16 asyncio.run(main())

[~/.local/lib/python3.9/site-packages/nest_asyncio.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/.local/lib/python3.9/site-packages/nest_asyncio.py) in run(future, debug)
     30         loop = asyncio.get_event_loop()
     31         loop.set_debug(debug)
---> 32         return loop.run_until_complete(future)
     33 
     34     if sys.version_info >= (3, 6, 0):

[~/.local/lib/python3.9/site-packages/nest_asyncio.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/.local/lib/python3.9/site-packages/nest_asyncio.py) in run_until_complete(self, future)
     68                 raise RuntimeError(
     69                     'Event loop stopped before Future completed.')
---> 70             return f.result()
     71 
     72     def _run_once(self):

[~/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/futures.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/futures.py) in result(self)
    199         self.__log_traceback = False
    200         if self._exception is not None:
--> 201             raise self._exception
    202         return self._result
    203 

[~/miniconda3/envs/py39ddmpeng/lib/python3.9/site-packages/playwright/_impl/_connection.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/miniconda3/envs/py39ddmpeng/lib/python3.9/site-packages/playwright/_impl/_connection.py) in wrap_api_call(self, cb, is_internal)
    510         self._api_zone.set(parsed_st)
    511         try:
--> 512             return await cb()
    513         except Exception as error:
    514             raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None

[~/miniconda3/envs/py39ddmpeng/lib/python3.9/site-packages/playwright/_impl/_connection.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/miniconda3/envs/py39ddmpeng/lib/python3.9/site-packages/playwright/_impl/_connection.py) in inner_send(self, method, params, return_as_dict)
     95         if not callback.future.done():
     96             callback.future.cancel()
---> 97         result = next(iter(done)).result()
     98         # Protocol now has named return values, assume result is one level deeper unless
     99         # there is explicit ambiguity.

[~/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/futures.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/futures.py) in result(self)
    199         self.__log_traceback = False
    200         if self._exception is not None:
--> 201             raise self._exception
    202         return self._result
    203 

[~/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/tasks.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/tasks.py) in __step(***failed resolving arguments***)
    254                 # We use the `send` method directly, because coroutines
    255                 # don't have `__iter__` and `__next__` methods.
--> 256                 result = coro.send(None)
    257             else:
    258                 result = coro.throw(exc)

[/tmp/ipykernel_188540/1335809689.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/tmp/ipykernel_188540/1335809689.py) in main()
      7 url = "https://blog.csdn.net/mopmgerg54mo/article/details/141028116"
      8 async def main():
----> 9     async with async_playwright() as p:
     10         browser = await p.chromium.launch(headless=True)
     11         page = await browser.new_page()

~/miniconda3/envs/py39ddmpeng/lib/python3.9/site-packages/playwright/async_api/_context_manager.py in __aenter__(self)
     44         if not playwright_future.done():
     45             playwright_future.cancel()
---> 46         playwright = AsyncPlaywright(next(iter(done)).result())
     47         playwright.stop = self.__aexit__  # type: ignore
     48         return playwright

[~/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/futures.py](http://map-search-jupyter-allhjasa-32-59-8897.intra.didiglobal.com/home/odin/miniconda3/envs/py39ddmpeng/lib/python3.9/asyncio/futures.py) in result(self)
    199         self.__log_traceback = False
    200         if self._exception is not None:
--> 201             raise self._exception
    202         return self._result
    203 

Exception: Connection closed while reading from the driver

Could you help me solve it? Thanks

https://github.com/ScrapeGraphAI/Scrapegraph-ai/issues/574

mxschmitt commented 2 months ago

Looks like a bug in Jupyter. I was running it without Jupyter and there it was working.

xjtupy commented 2 months ago

@mxschmitt I run it on linux and get this error

mxschmitt commented 2 months ago

Whats your host OS/distribution/version? We need more information about your environment. Please fill out the bug template the next time.

xjtupy commented 2 months ago

@mxschmitt Sorry for not posting in accordance with the regulations, which has caused you inconvenience.

environment:

playwright: 1.46.0
python:3.12
Browser: Chromium
linux:Linux version 3.10.0-693.21.1.el7.x86_64
mxschmitt commented 2 months ago

@mxschmitt Sorry for not posting in accordance with the regulations, which has caused you inconvenience.

environment:


playwright: 1.46.0

python:3.12

Browser: Chromium

linux:Linux version 3.10.0-693.21.1.el7.x86_64

Looks like your Linux distribution is not supported: https://playwright.dev/python/docs/intro#system-requirements