Using Playwright for Browser Automation: Form Submission, File Upload, Screenshots, Navigation, and Page Title Manipulation in Python
This article demonstrates how to use the Playwright Python library to automate browser actions such as submitting forms with file uploads, capturing screenshots, navigating between pages, and retrieving or setting the page title, providing complete code examples for each task.
Playwright is a powerful Python library for browser automation, offering a rich API to simulate user interactions like form submission, file upload, screenshot capture, page navigation, and title manipulation.
Form submission and file upload
import asyncio
from playwright.async_api import async_playwright
async def submit_form_and_upload_file(page, url, form_selector, file_path):
await page.wait_for_selector(form_selector)
form = await page.querySelector(form_selector)
await form.fill("username", "admin")
await form.fill("password", "123456")
file_input = await form.querySelector("input[type='file']")
await file_input.set_input_files([file_path])
await form.submit()
async def main():
async with async_playwright() as playwright:
page = await playwright.chromium().new_page()
await page.goto(url)
await submit_form_and_upload_file(page, "http://example.com/login", "#login_form", "path/to/file.txt")
await asyncio.sleep(5000)
await page.close()
asyncio.run(main())Screenshot capture
import asyncio
from playwright.async_api import async_playwright
async def save_screenshot(page, screenshot_path):
await page.waitForLoadState("networkidle")
screenshot = await page.screenshot()
with open(screenshot_path, "wb") as file:
file.write(screenshot)
async def main():
async with async_playwright() as playwright:
page = await playwright.chromium().new_page()
await page.goto("https://example.com")
await save_screenshot(page, "screenshot.png")
await page.close()
asyncio.run(main())Page navigation
import asyncio
from playwright.async_api import async_playwright
async def navigate_to_page(page, url):
await page.goto(url)
async def main():
async with async_playwright() as playwright:
page = await playwright.chromium().new_page()
target_url = "https://example.com"
await navigate_to_page(page, target_url)
await page.close()
asyncio.run(main())Getting the page title
import asyncio
from playwright.async_api import async_playwright
async def get_page_title(page):
title = await page.title()
print("页面标题:", title)
async def main():
async with async_playwright() as playwright:
page = await playwright.chromium().new_page()
await page.goto("https://example.com")
await get_page_title(page)
await page.close()
asyncio.run(main())Setting a new page title
import asyncio
from playwright.async_api import async_playwright
async def set_page_title(page, new_title):
await page.set_title(new_title)
async def main():
async with async_playwright() as playwright:
page = await playwright.chromium().new_page()
await page.goto("https://example.com")
new_title = "新的页面标题"
await set_page_title(page, new_title)
await asyncio.sleep(2000)
await page.close()
asyncio.run(main())Each example shows how to create a Playwright browser instance, perform the desired operation, and close the page, with notes to replace URLs, selectors, and file paths with actual values for real‑world use.
Test Development Learning Exchange
Test Development Learning Exchange
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.