Cross_Column

Monday, 23 March 2026

Browser and Page Handling in Playwright



Playwright with Python – Browser & Page Handling

In the previous tutorial, we learned about Playwright basics and installation. In this tutorial, we will explore how to launch browsers, open websites, handle multiple browsers, manage tabs, and navigate between pages using Playwright with Python.


Launching Browser in Playwright

Before interacting with any web application, we need to launch a browser instance. Playwright provides an easy way to launch different browsers such as Chromium, Firefox, and WebKit.

The following example shows how to launch a Chromium browser.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://example.com")
    browser.close()

Here we launch the browser, create a new page, navigate to a website, and finally close the browser.


Opening a Website

Playwright provides the goto() method to navigate to a specific URL.

page.goto("https://www.google.com")

This command opens the specified website in the browser window.


Headless vs Headed Browser Execution

Playwright allows tests to run in two modes:

  • Headless Mode – Browser runs in the background without a visible UI.
  • Headed Mode – Browser opens with a visible window.

Headless Execution

browser = p.chromium.launch(headless=True)

Headed Execution

browser = p.chromium.launch(headless=False)

Headless mode is generally used in CI/CD pipelines for faster execution.


Handling Multiple Browsers

Playwright supports multiple browser engines. This allows you to run tests across different browsers easily.

  • Chromium
  • Firefox
  • WebKit

Example:

with sync_playwright() as p:

    browser = p.chromium.launch()
    browser = p.firefox.launch()
    browser = p.webkit.launch()

This enables cross-browser testing using a single automation framework.


Creating Browser Context

A browser context in Playwright represents an isolated browser session. Each context has its own cookies, cache, and storage.

This is useful when you want to simulate multiple users in the same test.

browser = p.chromium.launch()

context = browser.new_context()

page = context.new_page()

page.goto("https://example.com")

Each browser context behaves like a separate browser profile.


Opening Multiple Tabs / Pages

Playwright allows you to open multiple tabs within the same browser context.

context = browser.new_context()

page1 = context.new_page()
page1.goto("https://google.com")

page2 = context.new_page()
page2.goto("https://bing.com")

In this example, two different tabs are opened in the same browser session.


Navigation Commands

Playwright provides various navigation commands to control browser movement.

  • page.goto(url) – Navigate to a website
  • page.go_back() – Navigate to the previous page
  • page.go_forward() – Navigate to the next page
  • page.reload() – Reload the current page

Example:

page.goto("https://google.com")

page.goto("https://github.com")

page.go_back()

page.go_forward()

page.reload()


Working with Google Chrome (Chromium)

Playwright uses the Chromium engine to automate browsers like Google Chrome and Microsoft Edge. Most automation tests are executed on Chromium because it is fast and widely used.

The following example launches a Chromium browser and opens a website.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:

    browser = p.chromium.launch(headless=False)

    page = browser.new_page()

    page.goto("https://www.google.com")

    print(page.title())

    browser.close()

This script launches the Chrome (Chromium) browser, opens Google, prints the page title, and closes the browser.


Working with Firefox

Playwright also supports the Mozilla Firefox browser. This allows testers to verify application behavior across different browser engines.

The following example launches Firefox using Playwright.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:

    browser = p.firefox.launch(headless=False)

    page = browser.new_page()

    page.goto("https://example.com")

    print(page.title())

    browser.close()

Running tests on Firefox helps ensure that your application behaves consistently across multiple browsers.


Working with WebKit (Safari Engine)

Playwright also supports the WebKit browser engine, which powers the Safari browser. This allows testers to perform cross-browser testing even for Safari environments.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:

    browser = p.webkit.launch(headless=False)

    page = browser.new_page()

    page.goto("https://example.com")

    print(page.title())

    browser.close()

Using WebKit helps ensure that your application works correctly on Safari-based browsers.

These commands allow testers to simulate real user navigation behavior.


Conclusion

Browser and page handling are fundamental concepts in Playwright automation. Understanding how to launch browsers, open websites, manage tabs, and navigate between pages is essential before performing advanced automation tasks.

In the next tutorial, we will explore one of the most important topics in Playwright automation: Locators and Element Identification.

Next Topic

No comments:

Post a Comment

Few More

Paraller Excution in PlayWright

Parallel Execution in Playwright with Python Automation test suites can become very large as the project grows. Runni...