Best Practices for Optimizing Page Waits and Asynchronous Operations with Playwright
This article explains how to improve Playwright automation scripts by using appropriate wait conditions, explicit timeouts, event listeners, custom functions, proper timeout settings, and parallel async execution, and provides clear Python code examples for each technique.
When using Playwright for web automation, optimizing page waits and handling asynchronous operations is crucial for script stability and execution efficiency.
1. Use appropriate wait conditions: Playwright offers various waiting options such as waiting for an element to appear, become visible, or disappear; select the condition that matches the specific scenario.
2. Explicit waits: For cases where a page needs extra time, use page.wait_for_timeout() to pause execution for a fixed duration before proceeding.
3. Handle asynchronous operations: Use page.wait_for_event() to wait for events like domcontentloaded or networkidle , ensuring the page or network activity has completed.
4. Parallel execution of async tasks: When multiple asynchronous actions are required, employ asyncio.gather() to run them concurrently, reducing overall wait time.
5. Use page state checks: For elements that appear or change after a delay, page.wait_for_function() allows you to define a custom predicate that repeatedly evaluates until the condition is satisfied.
6. Set appropriate timeouts: Adjust the default timeout with page.set_default_timeout() to avoid premature failures and handle timeout exceptions as needed.
Code examples:
from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto('https://example.com')
# 使用等待条件等待特定元素出现
element = page.wait_for_selector('h1')
print(element.text())
browser.close() from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto('https://example.com')
# 使用显式等待等待一段时间后继续执行
page.wait_for_timeout(5000) # 等待5秒
# 继续执行后续操作
element = page.query_selector('h1')
print(element.text())
browser.close() from playwright.sync_api import sync_playwright
with sync_playwright() as playwright:
browser = playwright.chromium.launch()
page = browser.new_page()
# 使用 wait_for_event() 等待页面的 domcontentloaded 事件
page.goto('https://example.com')
page.wait_for_event('domcontentloaded')
# 继续执行后续操作
element = page.query_selector('h1')
print(element.text())
browser.close() import asyncio
from playwright.async_api import async_playwright
async def perform_async_operations(page):
# 异步操作1
await page.click('button#button1')
# 异步操作2
await page.fill('input#input2', 'Hello')
# 异步操作3
await page.evaluate('document.querySelector("#element3").value = "World"')
async def main():
async with async_playwright() as playwright:
browser = await playwright.chromium.launch()
page = await browser.new_page()
await page.goto('https://example.com')
# 并行执行异步操作
await asyncio.gather(perform_async_operations(page))
# 继续执行后续操作
element = await page.query_selector('h1')
print(await element.text())
await browser.close()
asyncio.run(main())These examples demonstrate how to apply the suggested techniques in Playwright scripts to achieve more reliable and faster automation.
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.