how to close windows in selenium python without quitting the browser 1

how to close windows in selenium python without quitting the browser

for handle in driver.window_handles:
    driver.switch_to.window(handle)
    driver.close()

Here is what the above code is Doing:
1. We start by getting the current window handle, which will allow us to switch back to it later.
2. We then find all the handles, which returns a list.
3. We iterate over all the handles.
4. We switch to the new window.
5. We close the window.
6. We switch back to the original window.

Similar Posts