Getting Started with Playwright: Building a Simple Python Project, Page Navigation, Interaction, and Sync vs Async APIs
This tutorial walks you through creating a basic Playwright project in Python, demonstrates how to open web pages, retrieve titles, perform navigation and interactions, and explains the differences between Playwright's synchronous and asynchronous APIs.
In the previous article we covered installing and configuring Playwright; this guide dives deeper by showing how to create a simple Playwright project, open a web page, fetch its title, navigate, interact with elements, and understand the distinction between synchronous and asynchronous APIs.
1. Create a simple Playwright project – After confirming Playwright is installed, create a Python file (e.g., test_example.py ) and write the first script:
from playwright.sync_api import sync_playwright
def run(playwright):
# Launch browser instance
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
# Test logic
page.goto("http://example.com")
print(f"页面标题: {page.title()}")
# Close browser
browser.close()
with sync_playwright() as playwright:
run(playwright)This code launches a Chromium browser, navigates to a URL, and prints the page title.
2. Open a page and get its title – Using page.goto(url) loads the target URL, and page.title() returns the current page title. You can replace http://example.com with any site you wish to inspect.
3. Page navigation and basic interaction – Beyond simple navigation, Playwright can click buttons, fill inputs, and capture screenshots. Example:
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
# Navigate to Baidu
page.goto("https://www.baidu.com/")
# Fill search box
search_box = page.locator('input[name="wd"]') # Adjust selector if needed
search_box.fill("Playwright")
# Click search button
page.click('text=百度一下')
# Save screenshot
page.screenshot(path="baidu_search.png")
browser.close()
with sync_playwright() as playwright:
run(playwright)This script demonstrates locating an input field, entering text, clicking a button, and taking a screenshot.
4. Understanding synchronous vs asynchronous APIs – Playwright offers both models. The synchronous API blocks until I/O completes, while the asynchronous API allows other tasks to run concurrently, offering performance benefits for high‑concurrency scenarios.
Synchronous API example:
from playwright.sync_api import sync_playwright
def run(playwright):
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("http://example.com")
print(page.title())
browser.close()
with sync_playwright() as playwright:
run(playwright)Asynchronous API example:
import asyncio
from playwright.async_api import async_playwright
async def run(playwright):
browser = await playwright.chromium.launch(headless=False)
page = await browser.new_page()
await page.goto("http://example.com")
print(await page.title())
await browser.close()
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())Choose the API style based on personal preference or the need for handling many concurrent tasks.
Conclusion – After following this guide you should be able to set up a basic Playwright project, perform page navigation and interactions, and understand the core differences between its sync and async programming models. Future articles will cover advanced element locating techniques such as CSS selectors and XPath.
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.