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()
Copy

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.

Quiz on Selenium

Q1. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q2. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q3. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q4. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍