playwright
playwright copied to clipboard
[Feature] expose CDP network intiator for intercepted requests
As far as I can tell, there is no builtin way to access the request initiator with playwright.
This has been a long requested feature of puppeteer and it's only recently been added: https://github.com/puppeteer/puppeteer/issues/1395
There are many use cases for such a feature that of which a few are listed in that issue.
Here's an incomplete demo to get around this limitation. I used some of the code in #10464
import asyncio
from playwright.async_api import async_playwright
def intercept(r):
print(r["initiator"].get("url", "NOT A URL INITIATOR"))
async def set_interceptor(page):
# we need to enable the debugger and incrase the callstack depth to get accurate initiator results
# https://github.com/microsoft/playwright/issues/10464#issuecomment-976569128
cdp = await page.context.new_cdp_session(page)
await cdp.send("Network.enable")
await cdp.send("Debugger.enable")
await cdp.send("Debugger.setAsyncCallStackDepth", {"maxDepth": 32})
cdp.on("Network.requestWillBeSent", intercept)
async def run(playwright):
chromium = playwright.chromium
browser = await chromium.launch(headless=False)
page = await browser.new_page()
await set_interceptor(page)
await page.goto("https://old.reddit.com/r/videos/comments/wgi7ts/2002_an_unknown_band_called_the_killers_play/")
await browser.close()
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
Would this be something playwright would take a PR for? Or would the feature still need to be evaluated to be added?