For capturing prices after variant selection, Playwright is generally the better choice due to its auto-waiting, modern API, and built-in handling of dynamic content. Selenium can still work, but it requires more custom code for waiting and often struggles with modern JavaScript-heavy sites. This article explains the key differences, when each tool shines, and how to implement price scraping reliably.
Background: Why Variant Selection Is Tricky
E-commerce sites often update prices dynamically when you select a variant (size, color, storage, etc.) without reloading the page. The price element changes via JavaScript, often after an API call. Scraping this requires waiting for the new value to appear, not just the element to exist. Both Playwright and Selenium can do this, but their approaches and reliability differ significantly.
- Dynamic content: The price may be updated via AJAX or WebSocket, so you need to wait for the network response or a DOM change.
- Element re-rendering: The price element might be replaced entirely, so references to it become stale.
- Anti-bot measures: Some sites detect automated browsers; Playwright’s stealth options are more modern.
How Playwright Handles Variant Selection
Playwright is built for modern web apps. It uses a single WebSocket connection to control the browser, which is faster and more reliable than Selenium’s WebDriver protocol. Its key advantage is auto-waiting: actions like click() automatically wait for the element to be actionable, and waitForSelector() can wait for a specific text or state.
For price capture, you can do:
- Select the variant (e.g., choose size ‘L’ from a dropdown).
- Wait for the price text to change using
page.waitForFunction()orlocator.waitFor(). - Read the text content of the price element.
Playwright’s locator API is more robust than Selenium’s WebElement because it automatically re-queries the DOM if the element is detached. This handles re-rendering gracefully.
How Selenium Handles Variant Selection
Selenium has been around longer and supports multiple languages and browsers. However, it relies on the WebDriver protocol, which sends HTTP commands to the browser. This is slower and less reliable for modern SPAs. You often need to write explicit waits using WebDriverWait and expected_conditions.
A typical Selenium script might:
- Find the variant element and click it.
- Wait for a condition like
text_to_be_present_in_element. - Get the price text.
Problems arise because Selenium’s find_element returns a reference that can go stale if the page re-renders. You must re-locate the element or use more complex waits, which adds fragility.
Key Differences: Auto-Waiting and Speed
The biggest practical difference is auto-waiting. Playwright’s actions automatically wait for the element to be ready, reducing flakiness. Selenium requires explicit waits for almost everything, and even then, timing issues can occur.
- Wait strategy: Playwright waits for element to be visible, enabled, and stable. Selenium only checks existence unless you specify otherwise.
- Speed: Playwright is generally faster because it uses a more efficient protocol and can run multiple contexts in parallel.
- Network interception: Playwright can wait for specific network responses (e.g., the API call that returns the new price). Selenium cannot do this natively.
When to Use Selenium Instead
There are still cases where Selenium is preferable:
- Legacy environments: If you’re maintaining a large existing Selenium suite, migrating may not be worth it.
- Browser support: Selenium supports older browsers (e.g., IE) that Playwright does not.
- Language preference: Selenium has official bindings for Java, C#, Python, Ruby, and JavaScript. Playwright supports JavaScript/TypeScript, Python, Java, and .NET, but its Java and .NET support is newer.
- Team familiarity: If your team already knows Selenium, the learning curve for Playwright might be a temporary cost.
Common Pitfalls When Capturing Prices
Even with the right tool, you can hit issues:
- Stale elements: The price element may be replaced. Use Playwright’s
locatoror re-query in Selenium. - Timing: The price may update after a delay. Always wait for the specific text, not just for the element to exist.
- Currency formatting: Prices may include symbols, commas, or decimals. Extract the numeric value with a regex or by parsing.
- Multiple prices: Some pages show a range or a strikethrough price. Ensure you select the correct element.
- Anti-bot detection: Sites may block headless browsers. Playwright can use stealth plugins, but Selenium is more easily detected.
Cost and Maintenance Considerations
Both tools are free and open-source. The main cost is development and maintenance time. Playwright’s auto-waiting and better debugging (via trace viewer) can reduce debugging time. Selenium’s larger community means more examples and solutions, but its flakiness often leads to more maintenance.
If you’re building a new scraper, Playwright is the more future-proof choice. If you have an existing Selenium infrastructure, weigh the migration cost against the long-term reliability.
Frequently Asked Questions
Can I use Playwright with Selenium Grid?
No, Playwright has its own test runner and does not support Selenium Grid. However, Playwright has its own parallelization and can run on multiple browsers via its own configuration.
Does Selenium work better for sites with simple HTML?
For static sites, both work equally well. Selenium’s explicit waits are less of a burden when there’s no dynamic content. But Playwright’s auto-waiting still simplifies code.
How do I wait for the price to change after clicking a variant?
In Playwright, use page.waitForFunction() to wait for the text to change. In Selenium, use WebDriverWait with text_to_be_present_in_element.
Can I capture prices from a page that uses iframes?
Yes, both tools can switch to iframes. Playwright has a simpler frame_locator API, while Selenium requires switch_to.frame().
Is Playwright faster than Selenium for scraping?
Generally yes, because it uses a more efficient protocol and can run multiple contexts in parallel. However, network speed and page complexity matter more than the tool.
Which tool is better for a beginner?
Playwright’s API is more intuitive and has better documentation. Selenium has more tutorials, but its explicit waits can be confusing for beginners.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Best CS 1.6 Server Hosting: Options and How to Choose
- Best Secure Web Browsers for Privacy in 2026
- Nothing Phone 1: Specs, Updates and Buying Advice
- To-Do Lists for Mac: Apps, Features, and How to Choose
- Best Privacy Web Browsers: Which One Should You Use?
- Tower Defense Simulator TDS Codes: What Works Now