網路
簡介
Playwright 提供 API 來監控和修改瀏覽器網路流量,包括 HTTP 和 HTTPS。頁面執行的任何請求,包含 XHR 和 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"})
網路事件
- 同步
- 非同步
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() 等待 回應
- 同步
- 非同步
# 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 模式
Playwright 在網路攔截方法(如 page.route() 或 page.expect_response())中使用簡化的 glob 模式進行 URL 匹配。這些模式支援基本萬用字元
- 星號
- 單個
*
匹配除/
以外的任何字元 - 雙
**
匹配包括/
在內的任何字元
- 單個
- 問號
?
匹配除/
以外的任何單個字元 - 大括號
{}
可用於匹配以逗號,
分隔的選項列表
範例
https://example.com/*.js
匹配https://example.com/file.js
,但不匹配https://example.com/path/file.js
**/*.js
同時匹配https://example.com/file.js
和https://example.com/path/file.js
**/*.{png,jpg,jpeg}
匹配所有圖片請求
重要注意事項
- glob 模式必須匹配整個 URL,而不僅僅是部分 URL。
- 當使用 glob 進行 URL 匹配時,請考慮完整的 URL 結構,包括協定和路徑分隔符。
- 對於更複雜的匹配需求,請考慮使用 [RegExp] 而不是 glob 模式。
WebSockets
Playwright 支援開箱即用的 WebSockets 檢查、模擬和修改。請參閱我們的 API 模擬指南,以了解如何模擬 WebSockets。
每次建立 WebSocket 時,都會觸發 page.on("websocket") 事件。此事件包含 WebSocket 實例,用於進一步的 web socket 框架檢查
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)
遺失的網路事件與 Service Workers
Playwright 的內建 browser_context.route() 和 page.route() 允許您的測試原生路由請求並執行模擬和攔截。
- 如果您正在使用 Playwright 的原生 browser_context.route() 和 page.route(),並且發現網路事件遺失,請透過將 service_workers 設定為
'block'
來停用 Service Workers。 - 可能是您正在使用模擬工具,例如 Mock Service Worker (MSW)。雖然此工具可以開箱即用地用於模擬回應,但它會新增自己的 Service Worker 來接管網路請求,因此使其對 browser_context.route() 和 page.route() 不可見。如果您對網路測試和模擬都感興趣,請考慮使用內建的 browser_context.route() 和 page.route() 進行 回應模擬。
- 如果您不僅對使用 Service Workers 進行測試和網路模擬感興趣,而且對路由和監聽 Service Workers 本身發出的請求感興趣,請參閱 此實驗性功能。