Close specific Web page using Selenium in Python

Prerequisites: Selenium Basics, Selenium close() and quit() 

Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python.

The close() method is a web driver command which closes the browser window. During the automation process, if there are multiple browser windows opened, then the close() command will close the current browser window that is having attention at that time. 

Requirements:

You need to install chromedriver and set path. Click here to download.for more information follows this link.



Step-by-step Approach:

  • Import required modules

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import modules
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

chevron_right


  • Taking any URL.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# assign web page url

chevron_right


  • We want the child window of this web page then click on any button.

  •  Copy XPath.

  • Use the find_element_by_xpath() method to find XPath.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# find XPath
driver.find_element_by_xpath('//*[@id="Tabbed"]/a/button').click()

chevron_right


  • Create a handles variable who store all handles value of the open browser window.
  • Then the specified condition of the closing web page.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# return all handles value of open browser window
handles = driver.window_handles
  
for i in handles:
    driver.switch_to.window(i)
  
    # close specified web page
    if driver.title == "Frames & windows":
        time.sleep(2)
        driver.close()

chevron_right


The close() method closes current window page. Some pages are used for a long time some pages used for  short time. If we want to close a specific web page, we need to use some condition.

Implementation:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import modules
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
  
# use chrome driver
driver = webdriver.Chrome()
  
# assign web page url
  
# find XPath
driver.find_element_by_xpath('//*[@id="Tabbed"]/a/button').click()
  
# return all handles value of open browser window
handles = driver.window_handles
  
for i in handles:
    driver.switch_to.window(i)
  
    # close specified web page
    if driver.title == "Frames & windows":
        time.sleep(2)
        driver.close()

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.