0

I've run into a weird issue with python-playwright on my pc. Just yesterday everything was working fine, but now playwright throws errors at me for running with sync_playwight() as p:. This is what i narrowed the error down to:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    pass

Taceback:

Traceback (most recent call last):
  File "c:\Users\caspe\Documents\Programmeren\Python\test.py", line 3, in <module>
    with sync_playwright() as p:
  File "C:\Users\caspe\AppData\Roaming\Python\Python310\site-packages\playwright\sync_api\_context_manager.py", line 89, in __enter__
    playwright = self._playwright
AttributeError: 'PlaywrightContextManager' object has no attribute '_playwright'
Task was destroyed but it is pending!
task: <Task pending name='Task-3' coro=<Connection.run.<locals>.init() running at C:\Users\caspe\AppData\Roaming\Python\Python310\site-packages\playwright\_impl\_connection.py:222> wait_for=<Future pending cb=[Task.task_wakeup()]> cb=[ProtocolCallback.__init__.<locals>.cb() at C:\Users\caspe\AppData\Roaming\Python\Python310\site-packages\playwright\_impl\_connection.py:145]>
Future exception was never retrieved
future: <Future finished exception=BrokenPipeError(32, 'The pipe is being closed', None, 232, None)>
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\asyncio\proactor_events.py", line 397, in _loop_writing
    self._write_fut = self._loop._proactor.send(self._sock, data)
  File "C:\Program Files\Python310\lib\asyncio\windows_events.py", line 539, in send
    ov.WriteFile(conn.fileno(), buf)
BrokenPipeError: [WinError 232] The pipe is being closed

When I set a variable equal to sync_playwright() the type does become <class 'playwright.sync_api._context_manager.PlaywrightContextManager'> (I assume that is working as it should)

The only thing that changed is that i uninstalled anaconda a few days ago. If that was the issue, it'd be weird it'd only show up after a restart.

user15055153
  • 25
  • 1
  • 5

1 Answers1

1

I have run into a similar issue today after I uninstalled anaconda and tried to find solutions but turned out no methods to fix it, so I tried to debug the playwright code and found a quick fix.

My env infos are as follow:

Windows 10 pro 22H2
Python 3.8.16
pip 23.0.1
playwright 1.34.0.

My problem is that the playwright.cmd in \playwright\driver\ does not work, and it should call node to run cli.js to control the browser.

code in playwright.cmd:

setlocal
if not defined PLAYWRIGHT_NODEJS_PATH set PLAYWRIGHT_NODEJS_PATH=%~dp0node.exe
echo %PLAYWRIGHT_NODEJS_PATH% > D:\test.txt
echo %~dp0package\lib\cli\cli.js > D:\test.txt
"%PLAYWRIGHT_NODEJS_PATH%" "%~dp0package\lib\cli\cli.js" %*

It's quite simple, so what I did to fix the problem is directly call node.exe to run the script, which means that we need to modify the package source code. The key code in playwright is in the connect method of PipeTransport class in playwright\_impl\_transport.py, the quick fixing code is here:

async def connect(self) -> None:
    self._stopped_future: asyncio.Future = asyncio.Future()

    try:
        # For pyinstaller
        env = get_driver_env()
        if getattr(sys, "frozen", False):
            env.setdefault("PLAYWRIGHT_BROWSERS_PATH", "0")
        # modify to your own path
        path = r'C:\Users\username\.conda\envs\py38\Lib\site-packages\playwright\driver'
        self._proc = await asyncio.create_subprocess_exec(
            #! str(self._driver_executable),
            rf'{path}\node.exe',
            rf'{path}\package\lib\cli\cli.js',
            "run-driver",
            stdin=asyncio.subprocess.PIPE,
            stdout=asyncio.subprocess.PIPE,
            stderr=_get_stderr_fileno(),
            limit=32768,
            env=env,
        )
    except Exception as exc:
        self.on_error_future.set_exception(exc)
        raise exc

    self._output = self._proc.stdin

overwrite the connect method, and remember to modify the path to your own installed path of playwright package.

demo crazy
  • 11
  • 3