How to close the browser window at the end of a Selenium test program
In this tutorial, we are going to learn how to close the browser window at the end of a Selenium test program.
Selenium: Selenium is a Python library through which we can open and close URL links and browse web browsers using python script.
There is 2 method to close the browser window at the end of a Selenium test program.
- close()
- quit()
close() method in selenium
- It will close the window which is currently opened.
code:
#Installing selenium library !pip install selenium
output:
Collecting seleniumNote: you may need to restart the kernel to use updated packages. Downloading selenium-4.2.0-py3-none-any.whl (983 kB) Collecting trio~=0.17 Downloading trio-0.20.0-py3-none-any.whl (359 kB) Collecting trio-websocket~=0.9 Downloading trio_websocket-0.9.2-py3-none-any.whl (16 kB) Requirement already satisfied: urllib3[secure,socks]~=1.26 in c:\users\sumit\anaconda3\lib\site-packages (from selenium) (1.26.4) Requirement already satisfied: attrs>=19.2.0 in c:\users\sumit\anaconda3\lib\site-packages (from trio~=0.17->selenium) (20.3.0) Collecting outcome Downloading outcome-1.1.0-py2.py3-none-any.whl (9.7 kB) Requirement already satisfied: sniffio in c:\users\sumit\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.2.0) Requirement already satisfied: idna in c:\users\sumit\anaconda3\lib\site-packages (from trio~=0.17->selenium) (2.10) Requirement already satisfied: async-generator>=1.9 in c:\users\sumit\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.10) Requirement already satisfied: sortedcontainers in c:\users\sumit\anaconda3\lib\site-packages (from trio~=0.17->selenium) (2.3.0) Requirement already satisfied: cffi>=1.14 in c:\users\sumit\anaconda3\lib\site-packages (from trio~=0.17->selenium) (1.14.5) Requirement already satisfied: pycparser in c:\users\sumit\anaconda3\lib\site-packages (from cffi>=1.14->trio~=0.17->selenium) (2.20) Collecting wsproto>=0.14 Downloading wsproto-1.1.0-py3-none-any.whl (24 kB) Requirement already satisfied: certifi in c:\users\sumit\anaconda3\lib\site-packages (from urllib3[secure,socks]~=1.26->selenium) (2020.12.5) Requirement already satisfied: pyOpenSSL>=0.14 in c:\users\sumit\anaconda3\lib\site-packages (from urllib3[secure,socks]~=1.26->selenium) (20.0.1) Requirement already satisfied: cryptography>=1.3.4 in c:\users\sumit\anaconda3\lib\site-packages (from urllib3[secure,socks]~=1.26->selenium) (3.4.7) Requirement already satisfied: PySocks!=1.5.7,<2.0,>=1.5.6 in c:\users\sumit\anaconda3\lib\site-packages (from urllib3[secure,socks]~=1.26->selenium) (1.7.1) Requirement already satisfied: six>=1.5.2 in c:\users\sumit\anaconda3\lib\site-packages (from pyOpenSSL>=0.14->urllib3[secure,socks]~=1.26->selenium) (1.16.0) Collecting h11<1,>=0.9.0 Downloading h11-0.13.0-py3-none-any.whl (58 kB) Installing collected packages: outcome, h11, wsproto, trio, trio-websocket, selenium Successfully installed h11-0.13.0 outcome-1.1.0 selenium-4.2.0 trio-0.20.0 trio-websocket-0.9.2 wsproto-1.1.0
Steps:
- importing the webdriver from the selenium python library to open URL and time to sleep/wait our program for some time.
- path of chrome driver to pass into webdriver.
- we use chrome as a webdriver.
- Set URL.
- Opening the URL/website using the get() function.
- Now we want to sleep our program using the sleep() method for 10 sec to properly see our opening website.
- Now close() method close our current website.
# importing the webdriver from the selenium python library and time from selenium import webdriver #path path='C:\\Users\\sumit\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe' # Here we use chrome as a webdriver driver = webdriver.Chrome(path) # URL url = "https://www.codespeedy.com/" # Opening the URL/website driver.get(url) #sleep/wait our program for 10sec time.sleep(10) # Closing current window driver.close()
output:
Here we see our URL opens for 10 sec then the close() method closes our current webpage.
quit() method in selenium
- It will close the windows which are opened.
Code:
Steps:
- importing the webdriver from the selenium python library to open URL and time to sleep/wait our program for some time.
- path of chrome driver to pass into webdriver.
- we use chrome as a webdriver.
- Set URL.
- Opening the URL/website using the get() function.
- Now we want to sleep our program for 10 sec to properly see our opening website.
- Now quit() method close the websites.
# importing the webdriver from the selenium python library and time from selenium import webdriver #path path='C:\\Users\\sumit\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe' # Here we use chrome as a webdriver driver = webdriver.Chrome(path) # URL url = "https://www.codespeedy.com/" # Opening the URL/website driver.get(url) #sleep/wait our program for 10sec time.sleep(10) # Closing current window driver.quit()
Here we see our URL opens for 10 sec then the quit() method closes the webpage.
Thus, we have learned how to close the browser window at the end of a Selenium test program by two methods.
Leave a Reply