网络
简介
Playwright 提供了 API 来监控和修改浏览器的网络流量,包括 HTTP 和 HTTPS。页面发起的任何请求,包括 XHRs 和 fetch 请求,都可以被追踪、修改和处理。
模拟 API
请查看我们的 API 模拟指南 了解更多信息:
- 如何模拟 API 请求而不实际调用 API
- 执行 API 请求并修改响应
- 使用 HAR 文件来模拟网络请求。
HTTP 认证
执行 HTTP 认证。
- 同步
- 异步
context = browser.new_context(
http_credentials={"username": "bill", "password": "pa55w0rd"}
)
page = context.new_page()
page.goto("https://example.com")
context = await browser.new_context(
http_credentials={"username": "bill", "password": "pa55w0rd"}
)
page = await context.new_page()
await page.goto("https://example.com")
HTTP 代理
您可以配置页面通过 HTTP(S) 代理或 SOCKSv5 加载。代理可以为整个浏览器全局设置,也可以为每个浏览器上下文单独设置。
您可以选择性地为 HTTP(S) 代理指定用户名和密码,您还可以指定主机以绕过代理。
以下是一个全局代理的示例:
- 同步
- 异步
browser = chromium.launch(proxy={
"server": "http://myproxy.com:3128",
"username": "usr",
"password": "pwd"
})
browser = await chromium.launch(proxy={
"server": "http://myproxy.com:3128",
"username": "usr",
"password": "pwd"
})
也可以为每个上下文单独指定:
- 同步
- 异步
browser = chromium.launch()
context = browser.new_context(proxy={"server": "http://myproxy.com:3128"})
browser = await chromium.launch()
context = await browser.new_context(proxy={"server": "http://myproxy.com:3128"})
网络事件
您可以监控所有的 Requests 和 Responses:
- 同步
- 异步
from playwright.sync_api import sync_playwright, Playwright
def run(playwright: Playwright):
chromium = playwright.chromium
browser = chromium.launch()
page = browser.new_page()
# Subscribe to "request" and "response" events.
page.on("request", lambda request: print(">>", request.method, request.url))
page.on("response", lambda response: print("<<", response.status, response.url))
page.goto("https://example.com")
browser.close()
with sync_playwright() as playwright:
run(playwright)
import asyncio
from playwright.async_api import async_playwright, Playwright
async def run(playwright: Playwright):
chromium = playwright.chromium
browser = await chromium.launch()
page = await browser.new_page()
# Subscribe to "request" and "response" events.
page.on("request", lambda request: print(">>", request.method, request.url))
page.on("response", lambda response: print("<<", response.status, response.url))
await page.goto("https://example.com")
await browser.close()
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
或者在点击按钮后等待网络响应使用 page.expect_response():
- 同步
- 异步
# Use a glob url pattern
with page.expect_response("**/api/fetch_data") as response_info:
page.get_by_text("Update").click()
response = response_info.value
# Use a glob url pattern
async with page.expect_response("**/api/fetch_data") as response_info:
await page.get_by_text("Update").click()
response = await response_info.value
变体
使用 page.expect_response() 等待 Responses
- 同步
- 异步
# Use a regular expression
with page.expect_response(re.compile(r"\.jpeg$")) as response_info:
page.get_by_text("Update").click()
response = response_info.value
# Use a predicate taking a response object
with page.expect_response(lambda response: token in response.url) as response_info:
page.get_by_text("Update").click()
response = response_info.value
# Use a regular expression
async with page.expect_response(re.compile(r"\.jpeg$")) as response_info:
await page.get_by_text("Update").click()
response = await response_info.value
# Use a predicate taking a response object
async with page.expect_response(lambda response: token in response.url) as response_info:
await page.get_by_text("Update").click()
response = await response_info.value
处理请求
- 同步
- 异步
page.route(
"**/api/fetch_data",
lambda route: route.fulfill(status=200, body=test_data))
page.goto("https://example.com")
await page.route(
"**/api/fetch_data",
lambda route: route.fulfill(status=200, body=test_data))
await page.goto("https://example.com")
您可以通过在 Playwright 脚本中处理网络请求来模拟 API 端点。
变体
使用 browser_context.route() 或 page.route() 在整个浏览器上下文中设置路由。它将应用于弹出窗口和打开的链接。
- 同步
- 异步
context.route(
"**/api/login",
lambda route: route.fulfill(status=200, body="accept"))
page.goto("https://example.com")
await context.route(
"**/api/login",
lambda route: route.fulfill(status=200, body="accept"))
await page.goto("https://example.com")
修改请求
- 同步
- 异步
# Delete header
def handle_route(route):
headers = route.request.headers
del headers["x-secret"]
route.continue_(headers=headers)
page.route("**/*", handle_route)
# Continue requests as POST.
page.route("**/*", lambda route: route.continue_(method="POST"))
# Delete header
async def handle_route(route):
headers = route.request.headers
del headers["x-secret"]
await route.continue_(headers=headers)
await page.route("**/*", handle_route)
# Continue requests as POST.
await page.route("**/*", lambda route: route.continue_(method="POST"))
您可以继续修改请求。上面的示例从传出请求中删除了一个 HTTP 标头。
中断请求
您可以使用 page.route() 和 route.abort() 来中断请求。
- 同步
- 异步
page.route("**/*.{png,jpg,jpeg}", lambda route: route.abort())
# Abort based on the request type
page.route("**/*", lambda route: route.abort() if route.request.resource_type == "image" else route.continue_())
await page.route("**/*.{png,jpg,jpeg}", lambda route: route.abort())
# Abort based on the request type
await page.route("**/*", lambda route: route.abort() if route.request.resource_type == "image" else route.continue_())
修改响应
要修改响应,请使用 APIRequestContext 获取原始响应,然后将该响应传递给 route.fulfill()。您可以通过选项覆盖响应中的个别字段:
- 同步
- 异步
def handle_route(route: Route) -> None:
# Fetch original response.
response = route.fetch()
# Add a prefix to the title.
body = response.text()
body = body.replace("<title>", "<title>My prefix:")
route.fulfill(
# Pass all fields from the response.
response=response,
# Override response body.
body=body,
# Force content type to be html.
headers={**response.headers, "content-type": "text/html"},
)
page.route("**/title.html", handle_route)
async def handle_route(route: Route) -> None:
# Fetch original response.
response = await route.fetch()
# Add a prefix to the title.
body = await response.text()
body = body.replace("<title>", "<title>My prefix:")
await route.fulfill(
# Pass all fields from the response.
response=response,
# Override response body.
body=body,
# Force content type to be html.
headers={**response.headers, "content-type": "text/html"},
)
await page.route("**/title.html", handle_route)
Glob URL 模式
在拦截网络请求时,例如使用 page.route() 或 page.expect_response(),Playwright 使用简化版的通配符模式进行 URL 匹配。这些模式支持基本的通配符:
- 星号:
- 单个
*
匹配除/
之外的任何字符 - 双星号
**
匹配包括/
在内的任何字符
- 单个
- 问号
?
只匹配问号?
。如果你想匹配任何字符,请使用*
。 - 大括号
{}
可用于匹配由逗号,
分隔的选项列表 - 反斜杠
\
可用于转义任何特殊字符(注意反斜杠本身需要转义为\\
)
示例:
https://example.com/*.js
匹配https://example.com/file.js
但不匹配https://example.com/path/file.js
https://example.com/?page=1
匹配https://example.com/?page=1
但不匹配https://example.com
**/*.js
匹配https://example.com/file.js
和https://example.com/path/file.js
**/*.{png,jpg,jpeg}
匹配所有图片请求
重要提示:
- 通配符模式必须匹配整个 URL,而不仅仅是其中的一部分。
- 使用通配符进行 URL 匹配时,请考虑完整的 URL 结构,包括协议和路径分隔符。
- 对于更复杂的匹配需求,请考虑使用 [RegExp]。
WebSockets
Playwright 支持开箱即用的 WebSockets 检查、模拟和修改。请参阅我们的 API 模拟指南 了解如何模拟 WebSockets。
每当创建 WebSocket 时,都会触发 page.on("websocket") 事件。此事件包含进一步检查 web socket 帧的 WebSocket 实例:
def on_web_socket(ws):
print(f"WebSocket opened: {ws.url}")
ws.on("framesent", lambda payload: print(payload))
ws.on("framereceived", lambda payload: print(payload))
ws.on("close", lambda payload: print("WebSocket closed"))
page.on("websocket", on_web_socket)
缺失的网络事件和服务 Worker
Playwright 内置的 browser_context.route() 和 page.route() 允许您的测试本地化路由请求并执行模拟和拦截。
- 如果您正在使用 Playwright 的内置 browser_context.route() 和 page.route(),并且似乎缺少网络事件,请将 service_workers 设置为
'block'
以禁用服务 Worker。 - 您可能正在使用一个模拟工具如 Mock Service Worker (MSW)。虽然这个工具对于模拟响应来说即插即用,但它添加了自己的服务工作者接管了网络请求,从而使 browser_context.route() 和 page.route() 无法看到它们。如果您对网络测试和模拟都感兴趣,建议使用内置的 browser_context.route() 和 page.route() 进行 模拟响应。
- 如果您不仅对使用服务工作者进行测试和网络模拟感兴趣,还对路由和监听服务工作者本身的请求感兴趣,请参见 这个实验性功能。