Playwright その他の操作
イベント発生時に処理を行う
Playwrightで指定したイベントが発生したときに処理を行う方法です。
1回だけイベント処理を行う場合はPage.once()
を使用します。
サンプル
loadイベント、requestイベント、responseイベント発生時に処理を行うサンプルです。
const pw = require('playwright'); (async () => { const browser = await pw.chromium.launch({ headless: false }); const context = await browser.newContext(); const page = await context.newPage(); page.once('load', () => console.log('Page loaded!')); page.once("request", req => {console.log(req.method()) + " " + console.log(req.headers())}); page.once("response", res => {console.log(res.status() + " " + res.url())}); await page.goto('https://web.biz-prog.net/test/testpage.html'); await browser.close(); })();
private async void btnEvent_Click(object sender, EventArgs e) { using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() { Headless = false }); var page = await browser.NewPageAsync(); page.Load += (_, _) => Debug.WriteLine($"Page loaded!"); page.Request += (_, req) => Debug.WriteLine($"Request:{req.Method} {req.Url}"); page.Response += (_, res) => Debug.WriteLine($"Response:{res.Status} {res.Url}"); await page.GotoAsync("https://web.biz-prog.net/test/testpage.html"); }
from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch(headless=False) page = browser.new_page() page.once('load', print('Page loaded!')) page.once("request", lambda req : print("{} {}".format(req.method, req.headers))) page.once("response", lambda res : print("{} {}".format(res.status, res.url))) page.goto("https://web.biz-prog.net/test/testpage.html") browser.close()
継続してイベント処理を行う場合はPage.on()
を使用します。
処理が不要になればPage.off()
で解放します。
サンプル
const pw = require('playwright'); (async () => { const browser = await pw.chromium.launch({ headless: false }); const context = await browser.newContext(); const page = await context.newPage(); page.on("load", load); page.on("request", request); page.on("response", response); await page.goto('https://web.biz-prog.net/test/testpage.html'); page.off("load", load); page.off("request", request); page.off("response", response); await browser.close(); })(); function load() { console.log('Page loaded!') } function request(req) { console.log(req.method()) + " " + console.log(req.headers()) } function response(res) { console.log(res.status() + " " + res.url()) }
private async void btnEvent2_Click(object sender, EventArgs e) { using var playwright = await Playwright.CreateAsync(); await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() { Headless = false }); var page = await browser.NewPageAsync(); page.Load += Page_Load; page.Request += Page_Request; page.Response += Page_Response; await page.GotoAsync("https://web.biz-prog.net/test/testpage.html"); page.Request -= Page_Request; page.Response -= Page_Response; } private void Page_Load(object? sender, IPage e) { Debug.WriteLine($"Page loaded!"); } private void Page_Response(object? sender, IResponse e) { Debug.WriteLine($"Response:{e.Status} {e.Url}"); } private void Page_Request(object? sender, IRequest e) { Debug.WriteLine($"Request:{e.Method} {e.Url}"); }
from playwright.sync_api import sync_playwright def load(): print('Page loaded!') def request(req): print(req.method) print(req.headers) def response(res): print(res.status) print(res.url) with sync_playwright() as p: browser = p.chromium.launch(headless=False) page = browser.new_page() page.on('load', load) page.on("request", request) page.on("response", response) page.goto("https://web.biz-prog.net/test/testpage.html") page.remove_listener("load", load) page.remove_listener("request", request) page.remove_listener("response", response) browser.close()