启动 Playwright实例:sync_playwright().start()
终止 Playwright 实例:myPlaywright.stop()
Playwright 模块提供了一种启动浏览器实例的方法。以下是使用 Playwright 驱动自动化的典型示例:
from playwright.sync_api import sync_playwright# 启动 Playwright实例
myPlaywright = sync_playwright().start()# 启动浏览器实例
myBrowser = myPlaywright.chromium.launch(headless=False)
# myBrowser = myPlaywright.firefox.launch(headless=False)
# myBrowser = myPlaywright.webkit.launch(headless=False)# 获取设备字典信息,可被 browser.new_context(**kwargs)或browser.new_page(**kwargs) 使用
iphone = myPlaywright.devices["iPhone 6"]
# myBrowserContext = myBrowser.new_context(**iphone) # 创建新的浏览器上下文(对象):模拟指定设备# 连接到 APIRequest,可用于 Web API 测试的 API。
myRequest = myPlaywright.request# 连接到 选择器引擎,可用于 安装自定义选择器引擎。
mySelectors = myPlaywright.selectors# 终止 Playwright 实例
myPlaywright.stop()