Selenium Wrong Browser: Why It Happens and How to Fix
In this article
Selenium runs the wrong browser when it cannot correctly identify or locate the intended browser driver, or when default browser settings, environment variables, or WebDriver initialization code lead it to launch an unintended browser. This typically occurs due to misconfigured driver paths, ambiguous WebDriver instantiation, or system-level browser registration conflicts, and can be fixed by explicitly specifying the desired browser, ensuring correct driver versions, and validating environment setup.
Background: How Selenium Controls Browsers
Selenium WebDriver is a browser automation tool that communicates with web browsers through specific driver executables. Each major browser—Chrome, Firefox, Edge, Safari—requires its own dedicated driver: ChromeDriver for Chrome, GeckoDriver for Firefox, and so on. Selenium does not control browsers directly; instead, it sends commands via the WebDriver protocol to these standalone executables, which then translate them into actions within the actual browser.
The confusion about "the wrong browser" usually arises when a test script launches a browser different from the one intended by the developer. This is not due to Selenium randomly choosing browsers, but rather a consequence of how the WebDriver is initialized and how the system resolves browser and driver locations.
How Selenium Determines Which Browser to Launch
When you create a new WebDriver instance in your test script, Selenium relies on several factors to determine which browser to launch:
- Explicit browser specification: If you write
new ChromeDriver(), Selenium attempts to launch Chrome using ChromeDriver. - Driver executable location: Selenium needs to know where the driver (e.g., chromedriver.exe) is located. It may look in the system PATH, a specified path, or attempt to download it automatically via tools like WebDriverManager.
- Default browser settings: In some ambiguous configurations—especially when using generic WebDriver interfaces—Selenium may fall back to the system's default browser.
- Capabilities and options objects: These allow fine-grained control over which browser and version to use, including binary paths.
When any of these elements are missing or misconfigured, Selenium may launch a different browser than expected—often the system default or the first one it can find.
Common Causes of Selenium Launching the Wrong Browser
Several specific conditions can lead Selenium to start an unintended browser:
- Using
WebDriverwithout specifying a concrete driver: Code likeWebDriver driver = new ChromeDriver();is correct, but if mistakenly written asnew FirefoxDriver()or if the class import is wrong, the wrong browser launches. - Driver not in PATH and no explicit path set: If ChromeDriver isn't found, Selenium might silently fall back to another available driver or fail unpredictably.
- Multiple browser versions installed: Systems with Chrome, Chromium, and Edge all based on Chromium can confuse Selenium if the binary path isn't explicitly defined.
- Environment variables overriding settings: Variables like
webdriver.chrome.drivermight point to an outdated or incorrect driver. - Using Selenium Grid with misconfigured nodes: In distributed setups, the node might not have the intended browser installed or might map capabilities incorrectly.
A common example: a developer writes a test for Chrome, but on a machine where Firefox is default and no ChromeDriver is in PATH, the test might either fail or—depending on framework defaults—launch Firefox instead.
How to Fix Selenium Running the Wrong Browser
To ensure Selenium launches the correct browser, follow these steps:
- Explicitly instantiate the correct driver class: Always use
new ChromeDriver(),new FirefoxDriver(), etc., and verify imports are correct. - Set the driver executable path explicitly: Use
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");in Java, or pass the path directly in other languages. - Use WebDriverManager (Java): This library automatically downloads and sets up the correct driver version. For example:
WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); - Specify browser binary location: If multiple Chromium-based browsers exist, point directly to the executable:
ChromeOptions options = new ChromeOptions(); options.setBinary("/usr/bin/google-chrome"); WebDriver driver = new ChromeDriver(options); - Validate browser and driver versions: Ensure ChromeDriver version matches your Chrome browser version. Mismatches can cause failures or fallbacks.
- Use desired capabilities correctly: Define browser name, version, and platform in capabilities to avoid ambiguity in complex environments.
Additionally, always close existing driver instances before starting new ones, as lingering processes can interfere with new launches.
Comparing Browser Drivers and Compatibility
Each browser driver has specific compatibility requirements:
- ChromeDriver: Must match the major version of Google Chrome. Supports Chrome and Chromium-based browsers like Microsoft Edge (when configured).
- GeckoDriver: Required for Firefox. Works with Firefox ESR and regular releases, but older versions may lack support for new WebDriver features.
- EdgeDriver: Available in two forms—legacy Edge (EdgeHTML) and new Edge (Chromium). The latter uses ChromeDriver internally but has its own wrapper.
- SafariDriver: Built into macOS. Must be enabled via
safaridriver --enableand only supports macOS.
Mistaking one driver for another—such as using ChromeDriver for Firefox—will cause immediate failure. However, because Edge and Chrome are both Chromium-based, misconfiguration can sometimes result in Edge launching when Chrome was intended, or vice versa, especially if the binary path is not pinned.
Frequently Asked Questions
Why does Selenium open Firefox when I want Chrome?
This usually happens because you’re using new FirefoxDriver() instead of new ChromeDriver(), or your test framework’s default configuration points to Firefox. It can also occur if ChromeDriver is not in the system PATH and no explicit setup is done, causing the test to fall back to a working driver.
Can Selenium control multiple browsers at once?
Yes, Selenium can control multiple browser instances simultaneously, even of different types. You can run Chrome and Firefox tests in parallel by creating separate WebDriver instances for each. However, each requires its own correctly configured driver executable.
Does Selenium always need a downloaded driver executable?
Historically, yes—but modern tools like WebDriverManager (Java) or Selenium 4’s built-in driver management can automatically download and manage the correct driver. Still, you must ensure network access and correct version resolution to avoid mismatches.
Why does my Selenium test launch the browser but not navigate?
This may not be a browser selection issue but rather a driver-browser version mismatch, network blockage, or incorrect URL syntax. Ensure the browser and driver versions align, and check for errors in the console or logs that indicate connection timeouts or invalid arguments.
Can I use Selenium with headless browsers?
Yes, all major drivers support headless mode. For example, Chrome can run in headless mode using options.addArguments("--headless");. This doesn’t change the browser type but affects how it runs—useful for CI/CD pipelines.
Is it safe to let Selenium automatically download drivers?
Generally yes, if using trusted libraries like WebDriverManager, which pulls drivers from official sources (e.g., chromedriver.chromium.org). However, in secure environments, automatic downloads may be blocked, requiring manual driver placement and version control.