Frontend Development 5 min read

Using Playwright for Advanced UI Automation: Page Navigation, Alerts, Scrolling, Drag‑Drop, and Browser History

This article explains how to use Playwright with Python to automate complex web interactions such as multi‑page navigation, new‑tab handling, alert/confirm/prompt dialogs, scrolling, drag‑and‑drop, and browser history manipulation, providing clear code examples for each scenario.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Playwright for Advanced UI Automation: Page Navigation, Alerts, Scrolling, Drag‑Drop, and Browser History

In UI automation testing, accurately simulating user interactions with web pages—including navigation, tab management, dialog handling, scrolling, dragging, and history operations—is essential for realistic test coverage.

1. Page navigation and new‑tab management – Playwright offers straightforward APIs to open multiple pages and switch between them. Example code:

from playwright.sync_api import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    # Open the first page
    page1 = context.new_page()
    page1.goto("https://example.com")
    # Open a second page in a new tab
    page2 = context.new_page()
    page2.goto("https://another-example.com")
    # Switch back to the first page and perform actions
    page1.bring_to_front()
    print(page1.title())
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

This script opens two distinct pages and demonstrates switching focus between them.

2. Handling pop‑up dialogs (alert, confirm, prompt) – Many web apps use JavaScript dialogs for notifications or data collection. Playwright can listen for the dialog event and accept or dismiss as needed.

Alert example:

# Click the button that triggers an alert
page.click('button:has-text("Show Alert")')
# Accept the alert
page.on("dialog", lambda dialog: dialog.accept())

Confirm example:

# Click the button that triggers a confirm dialog
page.click('button:has-text("Show Confirm")')
# Dismiss the confirm dialog
page.on("dialog", lambda dialog: dialog.dismiss())

By attaching a listener to the dialog event and calling accept() or dismiss() , these dialogs can be managed automatically.

3. Advanced interactions: scrolling and dragging

Scrolling example:

# Scroll down 500 pixels
page.evaluate("window.scrollBy(0, 500)")
# Or scroll to a specific element
element = page.query_selector('your-selector')
element.scroll_into_view_if_needed()

Drag‑and‑drop example:

# Define source and target elements
source = page.query_selector('#draggable')
target = page.query_selector('#droppable')
# Perform the drag action
source.drag_to(target)

4. Browser history manipulation – Simulating forward and backward navigation mirrors real user behavior. Example:

# Navigate to the initial page
page.goto("https://example.com")
# Navigate to a second page
page.goto("https://another-example.com")
# Go back to the first page
page.go_back()
# Go forward to the second page
page.go_forward()

These commands let the test script move through the browser’s history stack.

Conclusion – By mastering these Playwright techniques—page navigation, dialog handling, scrolling, drag‑and‑drop, and history control—you can create more comprehensive and precise automation scripts that closely mimic real user actions. Future articles will cover network request interception to further enhance testing flexibility.

PythonUI automationPlaywrightweb testingBrowser Interaction
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.