1

I can't seem to find any information regarding Python's version of Puppeteer on how to check if my browser has closed properly, following browser.close().

I have limited knowledge of JavaScript, so can't properly follow the answer puppeteer : how check if browser is still open and working.

printing((browser.on('disconnected')) seems to return a function object, which when called requires something called f.

What is the proper way to check if the browser has closed properly?

from pyppeteer import launch 

async def get_browser():
    return await launch({"headless": False})

async def get_page():
    browser = await get_browser()
    url = 'https://www.wikipedia.org/'
    page = await browser.newPage()
    await page.goto(url)
    content = await page.content()
    await browser.close()
    print(browser.on('disconnected'))
    #assert browser is None
    #assert print(html)


loop = asyncio.get_event_loop()
result  = loop.run_until_complete(get_page())
print(result)
ggorlen
  • 44,755
  • 7
  • 76
  • 106
MasayoMusic
  • 594
  • 1
  • 6
  • 24

2 Answers2

1

.on methods register a callback to be fired on a particular event. For example:

import asyncio
from pyppeteer import launch 


async def get_page():
    browser = await launch({"headless": True})
    browser.on("disconnected", lambda: print("disconnected"))
    url = "https://www.wikipedia.org/"
    page, = await browser.pages()
    await page.goto(url)
    content = await page.content()
    print("disconnecting...")
    await browser.disconnect()
    await browser.close()
    return content


if __name__ == "__main__":
    result = asyncio.run(get_page())
    print(result)

Output:

disconnecting...
disconnected
<page content>

From the callback, you could flip a flag to indicate closure or (better yet) take whatever other action you want to take directly.

There's also browser.process.returncode (browser.process is a Popen instance). It's 1 after the browser has been closed, but not after disconnect.

Here's an example of the above:

import asyncio
from pyppeteer import launch 


async def get_page():
    browser = await launch({"headless": True})

    connected = True
    async def handle_disconnected():
        nonlocal connected
        connected = False

    browser.on(
        "disconnected",
        lambda: asyncio.ensure_future(handle_disconnected())
    )

    print("connected?", connected)
    print("return code?", browser.process.returncode)
    print("disconnecting...")
    await browser.disconnect()
    print("connected?", connected)
    print("return code?", browser.process.returncode)
    print("closing...")
    await browser.close()
    print("return code?", browser.process.returncode)


if __name__ == "__main__":
    asyncio.run(get_page())

Output:

connected? True
return code? None
disconnecting...
connected? False
return code? None
closing...
return code? 1
ggorlen
  • 44,755
  • 7
  • 76
  • 106
0

You can use browser. on('disconnected') to listen for when the browser is closed or crashed, or if the browser. disconnect() method was called. Then, you can automatically relaunch the browser, and continue with your program

MrEggo
  • 21
  • 3