为何不采用RemoteWebdriver driver = new ChromeDriver();实例化驱动?
Great question—this is such a common point of confusion when you're just starting out with Selenium, so let's break down each approach clearly.
First, let's recall the inheritance hierarchy in Selenium:
WebDriveris the top-level interface that defines all core browser automation methods (likefindElement(),get(),quit()).RemoteWebDriveris a concrete class that implementsWebDriver, handling remote session logic (it's the base class for all modern browser drivers like ChromeDriver, FirefoxDriver).ChromeDriver(and other browser-specific drivers) extendRemoteWebDriver.
Now let's look at why RemoteWebDriver driver = new ChromeDriver(); is less common, plus its pros and cons:
Pros of RemoteWebDriver driver = new ChromeDriver();
- Direct access to
RemoteWebDriver-specific methods: If you need to use features that only exist inRemoteWebDriver(likegetSessionId(),setFileDetector(), or configuring remote session parameters), this lets you call those methods without casting the driver object.
Cons of this approach
- Reduced flexibility: Unlike using the
WebDriverinterface, declaring the variable asRemoteWebDriverties you to a concrete class instead of an abstraction. While all major browser drivers do extendRemoteWebDrivernow, this couples your code to Selenium's internal implementation. If the framework ever changes (or if you want to use a driver that doesn't inherit fromRemoteWebDriver), you'd have to rewrite variable declarations across your codebase. - Over-specific for most use cases: 90% of the time, you only need the core methods defined in
WebDriver. Declaring asRemoteWebDriveradds unnecessary specificity without any real benefit for standard automation tasks.
Comparing to the more common approaches
1. WebDriver driver = new ChromeDriver();
This is the most recommended approach for beginners (and most projects). Here's why:
- Follows the "program to an interface, not an implementation" principle: You can switch between browser drivers (e.g., swap
ChromeDriverforFirefoxDriver) by only changing thenewstatement—no need to modify variable declarations anywhere else. - Keeps your code focused on core functionality: All the methods you'll use day-to-day (clicking elements, navigating pages, etc.) are defined in
WebDriver, so you don't lose any functionality.
2. ChromeDriver driver = new ChromeDriver();
Use this only when you need to call Chrome-specific methods (like interacting with Chrome's dev tools via getDevTools(), or using Chrome-only options that aren't exposed in WebDriver). The tradeoff is that your code is tightly coupled to Chrome—switching to another browser would require changing the variable type everywhere.
Final takeaway for beginners
Start with WebDriver driver = new ChromeDriver(); for most of your work—it gives you maximum flexibility and aligns with best practices. Use ChromeDriver (or another browser-specific driver) only when you need browser-exclusive features, and reserve RemoteWebDriver for cases where you specifically need its remote session capabilities.
内容的提问来源于stack exchange,提问作者sanjay

