3

I want to use playwright-python to fill some forms automatically. And then double check the filling before submit. But it always close the browser by the end of the code running. Even if I used the handleSIGHUP=False, handleSIGINT=False, handleSIGTERM=False launch arguments, and didn't use any page.close() or browser.close() in my code, it still close the browser after the code finished.

Does anyone know how to do it?

Kaol
  • 431
  • 2
  • 7
  • 16
  • in headed mode (headless=False), you can use page.pause(), it will show the playwright console. https://playwright.dev/python/docs/api/class-page#page-pause – MortenB Aug 14 '23 at 09:56

3 Answers3

2

The browser is started by the python script, so it will end, when the script ends.

So you need to keep the script alive. For the async part it is a bit tricky.

I assume you have same kind of: asyncio.get_event_loop().run_until_complete(main())

so in the async main routine you need to keep the async loop running:

e.g. waiting for the keyboard:

import asyncio
from playwright.async_api import async_playwright
from playwright.async_api import Page

async with async_playwright() as p:
    async def main(run_forever: bool):
        browser = await p.__getattribute__(C.BROWSER.browser).launch(headless=False, timeout=10000)
        page = await browser.new_page()
        if run_forever:
            print('Press CTRL-D to stop')
            reader = asyncio.StreamReader()
            pipe = sys.stdin
            loop = asyncio.get_event_loop()
            await loop.connect_read_pipe(lambda: asyncio.StreamReaderProtocol(reader), pipe)
            
            async for line in reader:
                 print(f'Got: {line.decode()!r}')
        else:
            browser.close()
Bill Charlaftis
  • 349
  • 1
  • 3
  • 14
HolgT
  • 663
  • 5
  • 18
0

get see worked link

Context:

Playwright Version: 1.16 Operating System: Windows Node.js version: 14.18.1 Browser: All Overview There are multiple ways to turn on the Inspector, as explained in https://playwright.dev/docs/inspector As per docs, when we set "PWDEBUG", it also disables the timeout, by setting it to 0. This is a good idea, as it allows the user to click around the inspector without a time limit.

However, this is not the case with page.pause(). This option also opens the inspector, but the timeout is not set to 0, thus the inspector will force exit after 30s with e.g.

Slow test: tests\my_test.test.js (30s)

The same behavior is mentioned in a Feature request here: #10132 And I believe it should be fixed to match the default behaviour of other options, at least for consistency reasons. For example, in Java binding, page.pause() does not require timeOut to be explicitly disabled.

0

you can use code below

while True:
    page.wait_for_timeout(10000)
fatelei
  • 76
  • 1
  • 4