- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to handle child windows in Selenium with python?
We can handle child windows or tabs in Selenium. While working with child windows, we need to always shift the browser focus to the child windows, then perform operation on them.
By default, the focus remains on the first parent window. There are multiple methods available in Selenium which are listed below −
current_window_handle
This method fetches the handle of the present window.
Syntax −
driver.current_window_handle
window_handles
This method fetches all the handle ids of the windows that are currently open.
Syntax −
driver.window_handles w = driver.window_handles[2]
The above code gives the handle id of the second window opened in the present session.
switch_to.window(args)
This method switches the focus of Selenium to the window name mentioned in the arguments.
Syntax −
driver.switch_to.window(childwindow)
The above code switches focus to the child window handle.
Example
Code Implementation with child window.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://the-internet.herokuapp.com/windows") #to refresh the browser driver.refresh() driver.find_element_by_link_text("Click Here").click() #prints the window handle in focus print(driver.current_window_handle) #to fetch the first child window handle chwnd = driver.window_handles[1] #to switch focus the first child window handle driver.switch_to.window(chwnd) print(driver.find_element_by_tag_name("h3").text) #to close the browser driver.quit()
- Related Articles
- How to handle popup windows in Selenium?
- How to handle frames in Selenium with python?
- How to handle windows file upload using Selenium WebDriver?
- Can selenium handle Windows based pop up?
- How will you handle alerts in Selenium with python?
- How do I create child windows with Python tkinter?
- Is it possible to handle Windows based pop-ups in Selenium?
- How to handle frames in Selenium Webdriver in Python?
- How will you travel from child to parent with xpath in Selenium with python?
- Handling Child Windows with Cypress
- How to handle frames in Selenium?
- How to Handle alerts in Selenium?
- How to handle "Plugin blocked" pop up using Selenium Python?
- How to handle chrome notification in Selenium?
- How to handle authentication popup with Selenium WebDriver using Java?
