Selenium – Close Browser Window

Selenium – Close browser window

In this tutorial, you will learn how to close browser window using Selenium in Python, with examples.

To close the browser window using webdriver object in Selenium, call close() function on the webdriver object.

driver.close()

Examples

1. Close Chrome browser window

In the following program, we initialize a webdriver, navigate to Python Examples home page https://pythonexamples.org, and then close the window using close() method.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Open a website
driver.get('https://pythonexamples.org')

# Close the browser window
driver.close()

When you run this program, a browser window is created, and upon the execution of close() method, the browser window would be closed.

Whatever the browser you would like to test on, the process is same. Just call the close() method on that webdriver object.

Summary

In this Python Selenium tutorial, we have seen how to close a browser window using Selenium webdriver object, with example programs.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us