Open In App

How to Switch between Window using Selenium in Python?

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.

handles=driver.window_handles

Implementation:




# 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:

Article Tags :