Open In App

How to Switch between Window using Selenium in Python?

Last Updated : 04 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we can be running with Python.

It’s miles fantastically viable that we open a massive range of windows. Each window might also additionally require us to carry out a few moves for finishing a stop-to-stop flow. For this, we need to be capable of transfer among them. We want to interchange over the manage additionally after which do the desired operation, because, with the aid of using the default, the point of interest stays at the figure window.

Requirement:

You need to install chromedriver and set path. Click here to download for more information follows this link. WebDriver supports moving between these windows using the “switch_to_window()” method.

driver.switch_to_window(“”)

Step-by-step approach:

If we open multiple windows then we want the print the title of every window page, but we print only a parent window of the title not an all window page title. This problem will solve through the driver.switch_to_window(“). Using this method we are capable of printing every page of the title.

  • Taking any URL.
  • We want the child window of this web page then click on any button using the find_element_by_xpath() method to find XPath.
  • Create a handles variable who store of all handles value of open browser window

handles=driver.window_handles

  • Then using loop print all the titles of webpages.

Implementation:

Python3




# import selenium module
from selenium import webdriver
  
# import select class
from selenium.webdriver.support.ui import Select
  
# using chrome driver
driver = webdriver.Chrome()
  
# web page url and open first window page
  
# find xpath of button for child window page
# this page no. 2
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)
  
    # print every open window page title
    print(driver.title)


Output:


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads