- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 −
This method fetches the handle of the present window.
This method fetches all the handle ids of the windows that are currently open.
driver.window_handles w = driver.window_handles
The above code gives the handle id of the second window opened in the present session.
This method switches the focus of Selenium to the window name mentioned in the arguments.
The above code switches focus to the child window handle.
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 #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()
- How to handle popup windows in Selenium?
- How to handle windows file upload using Selenium WebDriver?
- How to handle frames in Selenium with python?
- Can selenium handle Windows based pop up?
- How do I create child windows with Python tkinter?
- Handling Child Windows with Cypress
- How will you handle alerts in Selenium with python?
- 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?
- How to handle frames in Selenium?
- How to Handle alerts in Selenium?
- How to handle chrome notification in Selenium?
- How to handle authentication popup with Selenium WebDriver using Java?
- Handle Firefox Not Responding While Using Selenium WebDriver With Python?