- 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
What are the differences between current_window_handle and window_handles methods in Selenium with python?
There are differences between current_window_handle and window_handles methods in Selenium. Both are methods to handle multiple windows. They differences are listed below −
current_window_handle
This method fetches the handle of the present window. Thus it deals with the window in focus at the moment. It returns the window handle id as a string value.
Syntax −
driver.current_window_handle
window_handles
This method fetches all the handle ids of the windows that are currently open. The collection of window handle ids is returned as a set data structure.
Syntax −
driver.window_handles w = driver.window_handles[2]
The above code gives the handle id of the second window open in the present session.
Example
Code Implementation with current_window_handle
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 close the browser driver.quit()
Code Implementation with window_handles.
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 all handle ids of opened windows chwnd = driver.window_handles; # count the number of open windows in console print("Total Windows : "+chwnd.size()); #to close the browser driver.quit()
- Related Articles
- What are the differences between close() and quit() methods in Selenium with python?
- What are the differences between switch_to_default_content() and switch_to.parent_frame() methods in Selenium with python?
- What are the differences between readline() and readlines() in Selenium with python?
- What are the differences between xpath and css in Selenium with python?
- What are the differences between implicit and explicit waits in Selenium with python?
- What are the different ways of handling authentication popup window using Selenium?
- What are the differences between findElement() and findElements() methods?
- How to set window size using phantomjs and selenium webdriver in Python?
- How to switch to new window in Selenium for Python?
- How do I handle the window close event in Tkinter?
- what are the differences between unshift() and push() methods in javascript?
- How to avoid the pop-up window in chrome browser with Selenium?
- What are the differences between class methods and class members in C#?
- What are the differences between compareTo() and compare() methods in Java?\n
- How to handle ESC keydown on JavaScript popup window?

Advertisements