Using Playwright for UI Automation: Handling Dialogs, File Uploads, and Executing JavaScript
This article demonstrates how to use the Playwright library in Python to automate common UI interactions such as dismissing dialog boxes, uploading single or multiple files (including from a folder), and executing JavaScript code on web pages, providing complete code examples for each task.
UI automation typically involves interacting with page elements like dialogs, file inputs, and executing scripts; the following examples show how to accomplish these tasks using the Playwright library in Python.
1. Handling Dialogs
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
# Handle dialog
page.on('dialog', lambda dialog: dialog.dismiss()) # Auto‑close dialog
page.goto('https://example.com')
page.click('button')
browser.close()The code registers a listener for the dialog event and automatically dismisses any popup that appears.
2. Uploading Files
a. Single file upload
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
file_path = '/path/to/your/file.txt'
page.goto('https://example.com')
page.click('input[type=file]')
page.set_input_files('input[type=file]', file_path)
browser.close()b. Batch upload of multiple files
import os
from playwright.sync_api import sync_playwright
def upload_files(file_paths):
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
for file_path in file_paths:
file_name = os.path.basename(file_path)
page.goto('https://example.com/upload')
page.wait_for_selector('input[type=file]')
page.set_input_files('input[type=file]', file_path)
page.wait_for_load_state()
page.click('input[type=submit]')
print(f'Uploaded file: {file_name}')
browser.close()
file_paths = [
'/path/to/file1.txt',
'/path/to/file2.txt',
'/path/to/file3.txt',
]
upload_files(file_paths)This function iterates over a list of file paths, uploads each one, waits for completion, and prints the uploaded file name.
c. Batch upload from a folder
import os
from playwright.sync_api import sync_playwright
def upload_files_from_folder(folder_path):
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
file_paths = []
for root, dirs, files in os.walk(folder_path):
for file in files:
file_paths.append(os.path.join(root, file))
for file_path in file_paths:
file_name = os.path.basename(file_path)
page.goto('https://example.com/upload')
page.wait_for_selector('input[type=file]')
page.set_input_files('input[type=file]', file_path)
page.wait_for_load_state()
page.click('input[type=submit]')
print(f'Uploaded file: {file_name}')
browser.close()
folder_path = '/path/to/folder'
upload_files_from_folder(folder_path)The script walks through a directory, collects all file paths, and uploads each file using the same process as the batch upload example.
3. Executing JavaScript
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
# Execute JavaScript
page.goto('https://example.com')
result = page.evaluate('1 + 2')
print(result) # Output: 3
browser.close()Here page.evaluate() runs a JavaScript expression in the page context and returns the result.
These examples illustrate how Playwright can be used to automate common UI tasks; developers can adapt and extend the code to fit their specific testing or automation needs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
