Launching a Browser with Playwright
The Best Playwright Testing Training Institute in Hyderabad
In the fast-evolving field of software testing, tools like Playwright and Selenium are essential for automation testers. If you are a graduate, postgraduate, or someone looking to switch your career from a different domain to IT, it’s crucial to receive professional guidance and hands-on experience in automation tools. That’s where I Hub Talent excels. I Hub Talent is widely recognized as the best Playwright Testing Training Institute in Hyderabad. It offers a live, intensive internship program conducted by industry experts and specifically tailored for:
Launching a Browser – Step-by-Step
1. Basic Chromium Launch
python
Copy
Edit
import asyncio
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch() # Use .launch(headless=False) to see the browser
page = browser.new_page()
page.goto("https://example.com")
print(page.title())
browser.close()
2. Launching Firefox or WebKit
python
Copy
Edit
with sync_playwright() as p:
browser = p.firefox.launch(headless=False) # For Firefox
# or: browser = p.webkit.launch()
page = browser.new_page()
page.goto("https://example.com")
print("URL:", page.url)
browser.close()
3. Headless vs Headed Mode
Mode Use
headless=True (default) Runs without GUI (faster)
headless=False Opens real browser window (debugging/UI testing)
4. Controlling the Browser Context
python
Copy
Edit
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://google.com")
browser.close()
🌐 Useful Playwright Actions
Action Code
Type in input page.fill("input[name='q']", "Playwright")
Click a button page.click("text=Search")
Wait for element page.wait_for_selector("#element_id")
Take screenshot page.screenshot(path="screenshot.png")
Save PDF page.pdf(path="page.pdf") (only for Chromium)
🧰 Async Version (Optional)
Playwright also supports async:
python
Copy
Edit
import asyncio
from playwright.async_api import async_playwright
async def run():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False)
page = await browser.new_page()
await page.goto("https://example.com")
print(await page.title())
await browser.close()
asyncio.run(run())
🛠️ Troubleshooting
Problem Solution
Browser doesn’t launch Run playwright install again
Timeout errors Use page.set_default_timeout(ms)
Element not found Try using proper CSS/XPath or wait for selector
📌 Final Tips
Use sync version for simple scripts.
Use async for complex or parallel tasks.
Playwright is ideal for modern SPA apps and dynamic UIs.
Read more
Understanding Playwright Architecture
Setting Up Playwright with Node.js
Playwright with JavaScript vs Python vs TypeScript
Comments
Post a Comment