playwright-python uses expect_navigation instead of waitForNavigation

in JS

With Playwright (JS), to wait for the screen transition,

That is the standard.

const navigationPromise = page.waitForNavigation();
await page.click('input[type="submit"]'); //Events that cause screen transitions
await navigationPromise;

Or more simply

await Promise.all([
  page.waitForNavigation(),
  page.click('input[type="submit"]'),
]);

Write like this.

in Python

By the way, how should I write this kind of processing with the Sync API (API for those who do not use async/await) of playwright-python?

The correct answer (as of version 0.170.x) is

with page.expect_navigation():
  page.click('input[type="submit"]') #Events that cause screen transitions

How easy it is.

The waitFor somehow system methods are almost the same, just enclose it with the with statement and the expect_**** method, and it will do asynchronous processing nicely. It's wonderful. (Internal implementation, it seems that asyncio's Future is used)

Recommended Posts

playwright-python uses expect_navigation instead of waitForNavigation