In this article, we'd like to share the current state of Playwright integration with Python and several helpful code snippets for understanding the code techniques.
In comparison to other automation libraries like Selenium, Playwright offers:
- Native emulation support for mobile devices
- Cross-browser single API
- Microsoft Open Source team maintenance
- Scenarios that span multiple pages, domains, and iframes
- Auto-wait for elements to be ready before executing actions (like click, fill)
- Better developer experience by automatically installing the browsers
- Native input events for mouse and keyboard or up-/downloading files
And by that, all these features are also available in the Python integration. Be aware, that Playwright Python is currently in beta but already exposes many of the common methods and functions to be used. Since communication with browsers is mostly async based, Playwright does also provide an async based interface. You can pick the one that works best for you. They are identical in terms of capabilities and only differ in the way each consumes the API.
Also, most of these features are available in our API workers (except Microsoft maintenance).
Let's check out the main Playwright via the following examples:
This code snippet navigates to
scrapingant.com in Chromium, Firefox and WebKit, and saves 3 screenshots.
The code snippet below does the same as above, but in an async way.
Spoiler: Not as good as on a desktop one
But let's evaluate it with a screenshot! The following code snippet will help us to make a screen render from WebKit IPhone-like browser:
To know more about Python version of Playwright library just visit the official Github page: https://github.com/microsoft/playwright-python
And the original NodeJS version (Python and NodeJS APIs looks pretty much the same): https://github.com/microsoft/playwright
Also, the list of awesome Playwright resources: https://github.com/mxschmitt/awesome-playwright
The official website: https://playwright.dev/
The Python implementation of Playwright is still not so well-known and used as traditional NodeJS one, but Microsoft maintenance makes it better, more usable, and bug-free with frequent releases. Don't hesitate to help this awesome open-source library. If you encounter any bugs or find some missing features, feel free to file an issue on GitHub.
Our web scraping API runs thousands of headless browsers in the cloud, so you can just connect it and use without setting it up on your own.