Playwright for Python is a modern automation library that allows developers to control browsers
like Chromium, Firefox, and WebKit. It is widely used for testing, scraping, and simulating
real user interactions.
The package provides an asynchronous API, enabling fast and reliable automation. Developers
can launch browsers, navigate to pages, fill forms, click buttons, and capture results
with minimal code.
In practice, Playwright is useful for tasks such as automated testing, repetitive searches,
data collection, and simulating human-like browsing behavior across multiple browsers.
The example script demonstrates how to open Firefox, navigate to Google, perform a series
of searches, scroll the page, and pause between actions to mimic natural user activity.
Additionally, the script saves each search query into a text file, creating a simple log
of performed searches. This shows how Playwright can combine browser automation with
file handling for practical workflows.
I used the pip tool then I install the playwright:
pip install playwright
playwright installLet's see the script:
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.firefox.launch(headless=False)
context = await browser.new_context()
page = await context.new_page()
await page.goto("https://www.google.com")
queries = [
"python automation",
"playwright tutorial",
"google search automation"
]
with open("results.txt", "w", encoding="utf-8") as f:
for q in queries:
# Fill search box
await page.fill("textarea[name='q']", q)
await page.press("textarea[name='q']", "Enter")
await page.wait_for_load_state("domcontentloaded")
# Scroll + pause
await page.evaluate("window.scrollBy(0, document.body.scrollHeight)")
await page.wait_for_timeout(3000)
# Extract search results (titles + links)
results = await page.query_selector_all("h3")
f.write(f"\nResults for: {q}\n")
for r in results[:5]: # primele 5 rezultate
title = await r.inner_text()
link_el = await r.evaluate_handle("node => node.parentElement")
link = await link_el.get_attribute("href")
f.write(f"- {title} ({link})\n")
print(f"Saved results for: {q}")
await browser.close()
asyncio.run(main())
Then I run with this command:
python google_search_test_001.py
Saved results for: python automation
Saved results for: playwright tutorial
Saved results for: google search automationNeed to click to accept on browser ... , and some basic result on results.txt file:
Results for: python automation
Results for: playwright tutorial
- Playwright: Fast and reliable end-to-end testing for modern ...