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()
    
    try:
      driver.get("https://example.com")
      # Perform actions on the webpage
    
    except ConnectionClosedException:
      # Handle the exception by reopening the browser
      driver = webdriver.Chrome()
      driver.get("https://example.com")
  • 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")
    
    driver.get("https://example.com")
  • 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()
    
    try:
      driver.get("https://example.com")
      # Continue with automation
    
    except ConnectionClosedException:
      # Handle the exception or retry the operation
      pass
  • 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.
    try:
      driver.get("https://example.com")
      driver.execute_script("validJavaScriptCode();")
    
    except ConnectionClosedException:
      # Handle the exception or retry the operation
      pass
    	
    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()
    
    try:
      driver.get("https://example.com")
      WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "element_id")))
      # Continue with automation
    
    except TimeoutException:
      # Handle timeout or retry the operation
      pass
  • 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()
    
    chrome_options.add_argument("--disable-extensions")
    
    driver = webdriver.Chrome(chrome_options=chrome_options)
    
    driver.get("https://example.com")

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.

You're 15 Minutes Away From Automated Test Maintenance and Fewer Bugs in Production
Simply fill out your information and create your first test suite in seconds, with AI to help you do it easily and quickly.
Achieve More Than 90% Test Automation
Step by Step Walkthroughs and Help
14 Day Free Trial, Cancel Anytime
“We spent so much time on maintenance when using Selenium, and we spend nearly zero time with maintenance using testRigor.”
Keith Powe VP Of Engineering - IDT
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 ...
On our website, we utilize cookies to ensure that your browsing experience is tailored to your preferences and needs. By clicking "Accept," you agree to the use of all cookies. Learn more.
Cookie settings
Privacy Overview
This site utilizes cookies to enhance your browsing experience. Among these, essential cookies are stored on your browser as they are necessary for ...
Read more
Strictly Necessary CookiesAlways Enabled
Essential cookies are crucial for the proper functioning and security of the website.
Non-NecessaryEnabled
Cookies that are not essential for the website's functionality but are employed to gather additional data. You can choose to opt out by using this toggle switch. These cookies gather data for analytics and performance tracking purposes.