A Bit Of Context
In the automated web scraping industry, we’ve heard a lot of times about Selenium vs Playwright when there’s the need for a fully-headed scraper in Python (and of course Puppeteer for JS). And it is almost ironic that two of the most used tools were built for purposes other than web scraping.
Both Selenium and Playwright are, in fact, browser automation tools, created for helping front-end developers test their work, automating tests about the websites they are building using different browsers. But what is a scraper if not an automated browser going around the web? How to choose between Selenium vs Playwright?
What is Selenium
As mentioned before, webdriver Selenium is an open source automated testing framework used for cross-platform and cross-browser testing of web applications. It’s a web automation suite with several components and modules and you can find a great explanation of its history in this great blog post by Krishna Rungta.
For our web scraping purposes, what matters most is that it supports Firefox, Edge, Safari, and Chrome, via their webdrivers that need to be installed separately. A webdriver is a control interface for the browser, a sort of “remote controller” for browsers.
On a high level, a typical web scraper works like the following:
- Selenium WebDriver receives a command from the scraper
- Commands are converted into an HTTP request by the JSON wire protocol.
- Before executing any test cases, every browser has its driver which initializes the server.
- The browser then starts receiving the request through its driver.
What is Playwright
Playwright is an open source tool, a Node.js library started by Microsoft for automating browsers based on Chromium, Firefox, and WebKit through a single API, created by the same team that was working on Puppeteer in Google. The primary goal of Playwright is to improve automated UI testing. Playwright supports all the common web testing features like auto waiting and step by step debugging, fully aligned with the modern web.
It is of course very similar to Puppeteer, which works only with Chromium-based browsers and supports only Javascript language. In the automation test industry Playwright got a lot of good feedback for its speed: about this, there’s a great benchmark by Checkly that compares several frameworks for testing and automated actions, including Selenium, Playwright, and Puppeteer.
Its architecture is quite different from Selenium’s one because it interacts directly with slightly modified versions of the browsers bundled with the installation package via API, without the need for a Webdriver. This makes the setup pretty straightforward but doesn’t preclude the chance to interact with a standard Chrome instead of the bundled one.
Selenium vs Playwright: Features
Selenium, a pioneer in web automation, offers cross-browser and cross-platform compatibility. Its WebDriver API enables interaction with web elements, making it a versatile choice for various testing scenarios. Moreover, Selenium boasts a robust community, extensive documentation, and a wide array of language bindings, allowing developers to work in their preferred programming language.
On the other hand, Playwright, a more recent entrant, has quickly gained traction for its modern approach and advanced features. Developed by Microsoft, Playwright offers a unified API for automating browsers (Chromium, Firefox, and WebKit) and is known for its impeccable support of modern web technologies, including Single Page Applications (SPAs) and Progressive Web Apps (PWAs). Playwright’s intuitive syntax and built-in wait mechanisms contribute to faster and more reliable tests.
Selenium vs Playwright: Speed
When it comes to speed, Playwright has a distinct edge. Its architecture leverages modern browser capabilities like browser contexts and parallelism, resulting in significantly faster test execution. Playwright’s ability to run multiple instances of browsers in parallel and share browser contexts among tests minimizes overhead, thus reducing the overall test runtime. This can be especially advantageous for larger test suites or when aiming for quick feedback in a continuous integration pipeline.
Selenium, while still efficient, may not match the speed benefits offered by Playwright. Its architecture was conceived in an earlier era of web development, and though updates have improved its performance, it may not match the lightning-fast execution of Playwright for certain use cases.
Selenium vs Playwright: Performance
Playwright’s architecture inherently enhances performance by allowing tests to run with less overhead and resource consumption. The use of browser contexts and isolated environments ensures that tests remain independent and do not interfere with one another. This isolation contributes to consistent and reliable results.
Selenium, being a more established tool, has been widely adopted and proven over time. Its mature ecosystem and extensive community support provide a level of stability that can be reassuring for enterprise-level projects.
In conclusion, the choice between Selenium and Playwright hinges on the specific needs and priorities of your project. If you prioritize speed, advanced features, and modern web technology support, Playwright could be the preferred choice. On the other hand, if you value stability, extensive documentation, and a broad community, Selenium remains a robust option. Carefully evaluating your project’s requirements and conducting experiments with both tools can guide you toward the best fit for your web testing.
My Two Cents On Automated Web Scraping
You could have noticed that I often mentioned Puppeteer but it is not in the scope of this post. This is because it can be programmed only in Javascript and not in Python, which is the language I prefer. Yes, there is Pyppeteer but it’s an unofficial porting in Python of Puppeteer, and I still didn’t try it.
Restricting the comparison between Selenium vs Playwright, my personal choice falls on the second one. The easy setup and maintenance make the difference in a large web scraping project and the integration with other packages like playwright_stealth to avoid bot detections is quite straightforward. Both of these tools have adequate community support.
Being able to jump from one browser to another without the need to install anything make the fixing of scrapers fast and gives plenty of options. You can also use an installation of Chrome using a persistent context, which means you can have a real user profile for the whole execution of your scraper.
I’m leaving you this great article by Scrapfly where you can see how Playwright works and some code to test it.[/vc_column_text]
FAQ
Is Playwright better than Selenium?
Is Playwright built on Selenium?
Which tool is better than Selenium?
What are the disadvantages of Playwright?
What is replacing Selenium?
This article was kindly provided by Pierluigi Vinciguerra, web scraping expert and founder of Web Scraping Club. Follow this link to see the original post.
Download GoLogin here and explore the scraping world with our free plan!Reference source:
- Chapagain A. Hands-On Web Scraping with Python: Perform advanced scraping operations using various Python libraries and tools such as Selenium, Regex, and others. – Packt Publishing Ltd, 2019.
- Munzert S. et al. Automated data collection with R: A practical guide to web scraping and text mining. – John Wiley & Sons, 2014.
- Bansal M. et al. Data Ingestion and Processing using Playwright. – 2023.
- Egger R., Kroner M., Stöckl A. Web Scraping: Collecting and Retrieving Data from the Web //Applied Data Science in Tourism: Interdisciplinary Approaches, Methodologies, and Applications. – Cham : Springer International Publishing, 2022. – С. 67-82.
- Chaulagain R. S. et al. Cloud based web scraping for big data applications //2017 IEEE International Conference on Smart Cloud (SmartCloud). – IEEE, 2017. – С. 138-143.