Solving Selenium WebDriver NoSuchWindowException Error – no such window – window was already closed

NoSuchWindowException: no such window: window was already closed is an error which occurs in Selenium when it can not find the desired window.

In this article I am going to help you solve this error and understand the root of the problem, also I am presenting other possible solutions that may work if the main solution does not work for you.

Explaining the Error : Selenium WebDriver NoSuchWindowException Error – no such window – window was already closed

The error occurs in Selenium when Selenium can not find the desired window.

The error message should look like the error in the example bellow. Please make sure your error message is similar. The goal is to avoid confusing errors.

Selenium WebDriver NoSuchWindowException Error – no such window – window was already closed-1

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : handle the second window.

First of all, you need to understand that this error happens when the windows selenium should handle become two instead of one.

For example you click something and unexpectedly another window opens in the background and then closes. 

You can use the following code to solve the problem.

Selenium WebDriver NoSuchWindowException Error – no such window – window was already closed-1

Solution 2 : use WebDriverWait

The second solution is to use WebDriverWait when switching to a New Tab.

You should also use it again WebDriverWait when working with an iframe.

This is a snippet which shows how you can use WebDriverWait in your code.

Selenium WebDriver NoSuchWindowException Error – no such window – window was already closed-3

The solution above should work. You can also try the one bellow.

Solution 3 : update your chrome version.

Another solution is to run update your chrome version.

Sometimes a simple upgrade will remove the error entirely.

This should be enough to get rid of the error for good.

Summing-up

This is the end of our article, this is a confusing error but with a small investigation the error NoSuchWindowException: no such window: window was already closed could be solved, thank you for reading.

To support us consider donating to our Kofi account above. Keep coding, keep learning and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/