Cross_Column

Monday, 23 March 2026

Handling Web Components In PlayWright



Handling Web Components in Playwright with Python

Modern web applications contain many interactive components such as dropdowns, checkboxes, popups, frames, and file upload options. Automation testers must handle these elements effectively to create reliable test scripts.

Playwright provides powerful methods to interact with these web components easily. In this tutorial, we will learn how to handle different UI elements using Playwright with Python.


Handling Dropdowns

Dropdown elements allow users to select an option from multiple available values. Playwright provides the select_option() method to handle dropdown elements.

page.locator("#country").select_option("India")

You can also select options using value or label attributes.

page.locator("#country").select_option(label="India")

Handling Checkboxes

Checkboxes allow users to select one or multiple options. Playwright provides built-in methods to check and uncheck checkboxes.

page.locator("#subscribe").check()

To uncheck a checkbox:

page.locator("#subscribe").uncheck()

Handling Radio Buttons

Radio buttons allow users to select only one option from a group.

page.locator("#male").check()

Playwright uses the same check() method for radio buttons.


Handling Alerts / Popups

Alerts are browser popups that appear for confirmation, warnings, or notifications. Playwright allows handling alerts using event listeners.

page.on("dialog", lambda dialog: dialog.accept())

page.locator("#alert-button").click()

You can also dismiss alerts instead of accepting them.

page.on("dialog", lambda dialog: dialog.dismiss())

Handling Frames / iFrames

Frames are used to embed another webpage inside the main webpage. Playwright allows interaction with iframe elements using the frame_locator() method.

frame = page.frame_locator("#login-frame")

frame.locator("#username").fill("admin")

This allows automation scripts to interact with elements inside the iframe.


Handling New Windows / Tabs

Some applications open new browser windows or tabs when clicking certain links. Playwright can handle these easily using the expect_page() method.

with context.expect_page() as new_page_info:
    page.locator("#open-window").click()

new_page = new_page_info.value

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

This captures the new tab and allows automation scripts to interact with it.


File Upload

Playwright allows easy automation of file upload functionality using the set_input_files() method.

page.locator("#upload-file").set_input_files("testfile.pdf")

This automatically uploads the specified file to the input field.


File Download

Playwright can capture and manage file downloads triggered by the application.

with page.expect_download() as download_info:
    page.locator("#download-btn").click()

download = download_info.value

download.save_as("report.pdf")

This script waits for the download to start and saves the file locally.


Playwright Web Components – Quick Summary

The following table provides a quick reference for handling common web components in Playwright.

Component Playwright Method Description Example
Dropdown select_option() Select value from dropdown list page.locator("#country").select_option("India")
Checkbox check() / uncheck() Select or deselect checkbox page.locator("#subscribe").check()
Radio Button check() Select radio button option page.locator("#male").check()
Alert Popup page.on("dialog") Handle alert dialogs dialog.accept()
iFrame frame_locator() Interact with elements inside iframe page.frame_locator("#frame")
New Window expect_page() Handle new browser tab/window context.expect_page()
File Upload set_input_files() Upload file to application page.locator("#upload").set_input_files("file.pdf")
File Download expect_download() Capture and save downloaded files download.save_as("report.pdf")

Conclusion

Handling web components such as dropdowns, checkboxes, alerts, frames, and file uploads is an essential skill for automation engineers.

Playwright simplifies these tasks with built-in methods that make automation scripts reliable and easy to maintain.

In the next tutorial, we will learn about an important concept in automation testing: Waits and Synchronization in Playwright.

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...