Closing the Browser

Introduction

After completing an automation test, it is important to properly close the web browser. Closing the browser releases system resources, ends the current browser session, and ensures that subsequent test cases start with a clean environment.

Selenium provides the close() method to close the currently active browser window. If only one browser window is open, the browser closes, but the WebDriver session may still remain active until it is explicitly terminated.

Understanding when and how to use close() is essential for writing efficient and maintainable Selenium automation scripts.

In this tutorial, you’ll learn how to close the browser using Selenium WebDriver, understand the close() method, explore practical examples, compare it with quit(), and follow industry best practices.


What is close() in Selenium?

The close() method is used to close the currently active browser window.

If multiple browser windows or tabs are open, only the active window is closed. The remaining windows continue running, and the WebDriver session remains active.


Why Do We Need to Close the Browser?

Closing the browser helps to:

  • Release browser resources

  • Close unnecessary browser windows

  • Improve system performance

  • Prevent multiple browser windows from remaining open

  • Maintain clean test execution

In real-world automation projects, browsers should always be closed after completing the required operations.


Syntax

driver.close()

The close() method does not require any arguments.


Closing a Browser Window

Example

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://example.com")

driver.close()

Output

Chrome browser window closes.

Understanding the Code

Consider the following script:

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://example.com")

driver.close()

Let’s understand each step.

Launch Browser

driver = webdriver.Chrome()

Creates a new Chrome browser session.


Open Website

driver.get("https://example.com")

Loads the specified webpage.


Close Current Window

driver.close()

Closes the currently active browser window.


Browser Close Flow

When the following command executes:

driver.close()

The internal execution flow is:

Python Script
      │
      ▼
Selenium WebDriver
      │
      ▼
Browser Driver
      │
      ▼
Current Browser Window Closes

The browser window closes, but if multiple windows are open, the WebDriver session continues.


Practical Example

The following script opens Google’s homepage and then closes the browser window.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://www.google.com")

print(driver.title)

driver.close()

Output

Google

The current browser window closes after printing the page title.


Working with Multiple Browser Windows

Suppose your automation opens two browser windows.

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://example.com")

driver.switch_to.new_window("window")

driver.get("https://www.python.org")

driver.close()

In this example:

  • The second browser window closes.

  • The first browser window remains open.

  • The WebDriver session is still active.


Automation Testing Example

Suppose you’re testing a login popup that opens in a separate browser window.

After completing the validation, you only want to close the popup window.

driver.close()

The main application window remains open for further testing.


Real-World Example

Imagine you’re automating an online banking application.

The application opens a separate window for downloading account statements.

Automation flow:

  • Open the main banking website.

  • Open the statement window.

  • Download the statement.

  • Close only the statement window.

  • Continue working in the main application.

This is a common use case for driver.close().


Difference Between close() and quit()

Featureclose()quit()
Closes current browser windowYesYes
Closes all browser windowsNoYes
Ends WebDriver sessionNoYes
Used for multiple windowsYesNo (closes everything)

In most automation frameworks, quit() is used at the end of the test, while close() is useful when only a specific window needs to be closed.


Common Mistakes Beginners Make

Confusing close() with quit()

Many beginners assume both methods perform the same action.

Remember:

  • close() closes only the active browser window.

  • quit() closes all browser windows and ends the WebDriver session.


Performing Operations After Closing the Window

Incorrect:

driver.close()

driver.title

Error

NoSuchWindowException

Once the current window is closed, Selenium cannot interact with it.


Forgetting to Switch Windows

If multiple browser windows are open, always switch to the desired window before calling close().


Best Practices

  • Use close() only when you need to close the current browser window.

  • Use quit() at the end of the entire automation test.

  • Switch to the correct browser window before closing it.

  • Avoid leaving unused browser windows open during automation.

  • Always manage browser windows carefully in multi-window scenarios.


Conclusion

The close() method is used to close the currently active browser window without ending the entire WebDriver session. It is particularly useful when working with multiple browser windows or tabs, where only one window needs to be closed while continuing automation in the remaining windows.

Understanding the difference between close() and quit() is an important part of writing reliable Selenium automation scripts.


Frequently Asked Questions (FAQs)

What does driver.close() do?

It closes the currently active browser window.


Does close() end the WebDriver session?

No.

The WebDriver session continues if other browser windows are still open.


When should I use close()?

Use close() when you want to close only the current browser window while keeping the remaining windows open.


What happens if only one browser window is open?

The browser window closes, but the WebDriver session may still exist until quit() is called.


What is the difference between close() and quit()?

  • close() closes only the current browser window.

  • quit() closes all browser windows and completely ends the WebDriver session.


Key Takeaways

  • The close() method closes the currently active browser window.

  • It is useful when working with multiple browser windows or tabs.

  • close() does not terminate the WebDriver session if other windows remain open.

  • Use close() to close specific windows during automation workflows.

  • Use quit() to close all browser windows and end the WebDriver session after test execution.

  • Understanding the difference between close() and quit() is essential for effective browser window management in Selenium.