user901
user901

Reputation: 75

Selenium switch to popup window

When clicking a radio button, a new popup window opens and Selenium is unable to locate any elements from the new popup window. How do I switch Selenium to the new popup window, or extract the URL of the popup window?

print(driver.current_url)

retrieves the URL of the old tab and not the popup window.

print(driver.title)

prints the title of the old tab and not the popup window.

Thanks in advance.

Upvotes: 3

Views: 6028

Answers (1)

tbjorch
tbjorch

Reputation: 1758

When you open the popup you probably get an additional window handle and need to change window handle to interact with the popup. This can be checked in the drivers window_handles attribute, and switched to by using the drivers switch_to_window method.

# before clicking button to open popup, store the current window handle
main_window = driver.current_window_handle

# click whatever button it is to open popup

# after opening popup, change window handle
for handle in driver.window_handles: 
    if handle != main_window: 
        popup = handle
        driver.switch_to_window(popup)

print(driver.title) # Should now be the popup window

Upvotes: 7

Related Questions