In this blog, we are working on how to close the page and browser and terminate the session.
As a pre-requisite we have to check whether the following dependencies or installed or not:
- Project setup.
- Run pip list in PyCharm terminal to check whether playwright is installed or not.

As shown in the above picture playwright should be installed in the project.
If the playwright is not installed refer to Web Automation Testing using Playwright.
The necessity of page or browser close:
When we launch the page or browser, we create the browser session, to terminate the browser session we have to close the page or browser.
Methods to close the browser and page in Playwright:
1. Page.close(): –
In page.close() method it closes the browser page. In the .close() method there is a run_before_unload parameter by default it is set to false. If runBeforeUnload is false, then it does not run any unload handlers and waits for the page to be closed. If runBeforeUnload is true, the method will run unload handlers, but will not wait for the page to close.

2. Browser.close(): –
The browser object which is used to launch the browser, the playwright disposes of the browser object when calling browser.close(). In browser.close() it closes all the pages of the browser. If the browser is connected to the server and logged into any site, then it clears all the contexts belonging to this browser and auto disconnects from the browser server. This is similar to forcefully quitting the browser.

Test Case Sync API:
Step 1: – Launch the Browser.
Step 2: – Navigate to https://www.google.com
Step 3: – Fill in welcome to close ops in the playwright text in the Input box.
Step 4: – close the page and browser.
from playwright.sync_api import sync_playwright
with sync_playwright() as page:
browser = page.chromium.launch(headless=False)
page = browser.new_page()
page.set_viewport_size({"width":1550,"height":800})
page.goto('https://www.google.com')
page.locator(".gLFyf.gsfi").fill("welcome to close ops in playwright")
page.wait_for_timeout(3000)
page.close(run_before_unload=False)
browser.close()

In the above code snippet,
- The statement from line no. 3 to 6 is used to launch, maximize the browser and create the browser session.
- The statement in line no. 7 is used to fill the text in the search box.
- The statement from line no. 9 to 10 is used to close the page, and browser and terminate the session.
Test Case Async API:
Step 1: – Launch the Browser.
Step 2: – Navigate to https://www.google.com
Step 3: – Fill in welcome to close ops in the playwright text in the Input box.
Step 4: – close the page and browser.
import asyncio
from playwright.async_api import async_playwright
async def close_browser():
async with async_playwright() as page:
browser = await page.chromium.launch(headless=False)
page = await browser.new_page()
await page.set_viewport_size({"width": 1550, "height": 800})
await page.goto('https://www.google.com')
await page.locator(".gLFyf.gsfi").fill("welcome to close ops in playwright")
await page.wait_for_timeout(3000)
await page.close(run_before_unload=False)
await browser.close()
asyncio.run(close_browser())

In the above code snippet,
- The statement from line no. 5 to 8 is used to launch, maximize the browser and create the browser session.
- The statement in line no. 9 is used to fill the text in the search box.
- The statement from line no. 11 to 12 is used to close the page, and browser and terminate the session.
Read the next Playwright blog Here.