Turn your manual testers into automation experts! Request a DemoStart testRigor Free

ConnectionClosedException in Selenium Explained

Exceptions are unexpected errors that occur while executing the Selenium automation scripts. These exceptions are raised when the WebDriver faces issues with web pages and UI elements. Using automation scripts, you can catch and handle these exceptions to implement error recovery. Also, this helps in providing meaningful error messages on test failure.

Today, we will discuss the ConnectionClosedException, its root cause, and the best practices to avoid this exception.

ConnectionClosedException – Root Cause

The ConnectionClosedException is thrown when there is an unexpected termination of the communication channel between the Selenium WebDriver and the web browser. There are several reasons this exception is thrown. Let’s review the common ones.

  • Browser is Crashed or Closed: When the web browser where the test scripts are getting executed gets closed or crashes unexpectedly, this exception will be thrown.
  • Mismatch in WebDriver Version: This exception will be thrown when there is a mismatch of versions or when we use an incompatible WebDriver version with the browser.
  • Facing Network Issues: Any issues with the network, such as network interruptions, unstable internet, or configuration issues, can break the communication between WebDriver and the browser, leading to ConnectionClosedException.
  • JavaScript Issues: If the web page contains JavaScript errors while executing test automation, there can be connection issues between WebDriver and the browser. This can throw ConnectionClosedException.
  • Resource Exhaustion: Running resource-intensive scripts or operating in an environment with limited system resources can cause the browser instance to become unresponsive and result in a ConnectionClosedException.
  • Incorrect WebDriver Initialization: Incorrectly configuring or initializing the WebDriver with the wrong options, capabilities, or settings can lead to communication problems with the browser, resulting in a ConnectionClosedException.

Resolving ConnectionClosedException

  • Handle Browser Closure: If the exception is triggered due to the unexpected closure of the browser, then we can add logic to check whether the browser is still open before interacting with it.
    driver = webdriver.Chrome()
      # Perform actions on the webpage
    except ConnectionClosedException:
      # Handle the exception by reopening the browser
      driver = webdriver.Chrome()
  • Check WebDriver Version Mismatch: Ensure the WebDriver and the browser versions are compatible. It’s always better to download the correct WebDriver version and specify the initializing path.
    driver = webdriver.Chrome(executable_path="path/to/correct/chromedriver.exe")
  • Resolve Network Issues: Network issues can be handled by improving the network connection stability, or else we need to handle this scenario by adding an appropriate try-catch block like this:
    driver = webdriver.Chrome()
      # Continue with automation
    except ConnectionClosedException:
      # Handle the exception or retry the operation
  • Handle JavaScript Errors: Always ensure there are no JavaScript errors for the automated page. Before running the test scripts, fix the errors or add a try-catch block to handle the exception.
    except ConnectionClosedException:
      # Handle the exception or retry the operation
    driver = webdriver.Chrome()
  • Manage Resources: Monitoring and managing system resources is always good to prevent resource exhaustion. Also, ensure that we are not running resource-intensive scripts simultaneously.
  • Optimize Timeouts and Waits: We must ensure enough timeouts and waits for the elements to load. Otherwise, these timeouts can cause exceptions.
    driver = webdriver.Chrome()
      WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "element_id")))
      # Continue with automation
    except TimeoutException:
      # Handle timeout or retry the operation
  • Handle Browser Extensions: It’s always safe to turn off browser extensions that may interfere with test execution. We can turn off extensions by passing the argument “–disable-extensions”.
    chrome_options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(chrome_options=chrome_options)

Switching to Smart AI-powered Automation

Selenium is one of the early-generation automation tools used by most companies. During that period, the development model mainly followed the waterfall model, so the automation team had enough time to create the framework, build integrations, etc.

But now, the process is agile or extreme programming, where testing is the core part. The application or features are developed, tested, and released to the market on a tight schedule. So the automation team doesn’t have time to run behind the exceptions. They need an intelligent tool that can handle all the requirements mentioned above. The team needs to focus only on creating the script. Selenium couldn’t walk along the much-needed Shift Left approach and thus failed miserably. This is where intelligent AI-powered tools such as testRigor stand out.

testRigor is a feature-rich tool that makes test automation smarter. Let’s check that.

  • No Installation Required: testRigor is a cloud-hosted tool. So, creating, installing, and maintaining the infrastructure is not needed.
  • No Custom Framework: testRigor has its inbuilt framework, so there is no need to create a custom framework or modify the existing one.
  • No Need to Download Drivers: We need to mention which browser is required to run the automation; testRigor takes care of the rest of it. So we can avoid these exceptions in testRigor.
  • Auto Page Waits: We don’t have to wait for the page to load manually. testRigor has a framework that will automatically wait until the page is fully loaded. Then, only the execution starts. So, no errors or exceptions can happen due to page loads.
  • Making Automation Easy:
    • testRigor helps create test scripts in parsed English; hence, there is no dependency on learning programming languages.
    • Also, testRigor doesn’t use flaky XPaths to identify elements. Instead, it uses the UI text as visible to the human eye to identify elements. testRigor locators are ultra-stable and are empowered by self-healing capabilities.
You can check out the top features of testRigor. Here is a sample testRigor test script in plain English:
open url "https://www.loginurl.com"
enter stored value "UserName" into "User Name"
enter stored value "password" into "Password"
click "Sign in"
click “Employees”
click “Add New Employee”
enter "John" into "First Name"
enter "Doe" into "Last Name"
click "Submit"

The example above clearly shows the simplicity of testRigor test script creation, maintenance, and other test activities as automation progresses to the next level.

End Notes

The current market trend is “More Features, More Releases”. Undoubtedly, every organization is trying to achieve this to stay ahead of its competitors. To support this entirely, testing plays a vital role, as it guarantees the high quality of the product within a short time frame.

Intelligent testing tools such as testRigor are super helpful in achieving excellent testing quality within budget. Its simplicity helps the QA team achieve expansive test coverage quickly with minimum effort and maintenance. Therefore, it helps achieve superior product quality faster before delivery.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.
Related Articles

SAP Concur Testing

Concur Technologies, now SAP Concur, is an American software-as-a-service (SaaS) company providing travel and expense management ...

Authorize.net Testing

You need a payment gateway in several scenarios, especially when you are running a business that requires secure, efficient, and ...

KYC Portal CLM Testing

KYC Portal CLM is an award-winning Client Lifecycle Management (CLM) platform designed to streamline and automate the Know Your ...