Selenium for Java and Ruby have methods to get current window handle.
For example, in Java it's pointed here.
In the same time Pythonic version of Selenium has no such method.
The window handle is a unique identifier that stores the values of windows opened on a webpage and helps in window handling in Selenium. getWindowHandles( ) and getWindowHandles( ) handle windows in Selenium. The user has to switch from the parent window to the child window to work on them using switchTo( ) method.
Get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver. getWindowHandles(); which returns the set of handles. Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.
Get window handleIf your site opens a new tab or window, Selenium will let you work with it using a window handle. Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using: Java. Python.
There is current_window_handle
property available on the WebDriver
instance:
driver.current_window_handle
Demo:
>>> from selenium import webdriver
>>>
>>> driver = webdriver.Chrome()
>>> driver.get('https://stackoverflow.com')
>>>
>>> driver.current_window_handle
CDwindow-B22C1E54-977D-4B2A-8048-E9C73999E9C7
To get the name of the window in python but using javascript you can do this in python: ( I looked for this everywhere, but no answer was given for this)
window_title = driver.execute_script("return window.document.title")
window_name = driver.execute_script("return window.name") # e.g. 'win1'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With