How to Switch between Window using Selenium in Python?
Last Updated :
04 Jan, 2021
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
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
driver.find_element_by_xpath( '//*[@id="Tabbed"]/a/button' ).click()
handles = driver.window_handles
for i in handles:
driver.switch_to.window(i)
print (driver.title)
|
Output:
Share your thoughts in the comments
Please Login to comment...