Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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()Advertisements