AlexKorovyansky
AlexKorovyansky

Reputation: 4943

Python Selenium get current window handle

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.

  1. Maybe it's inside, but I don't see it?
  2. If it's skipped, is it reasonable and how to make workaround for it?

Upvotes: 18

Views: 33160

Answers (1)

alecxe
alecxe

Reputation: 473763

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'

Upvotes: 20

Related Questions