4

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.

1
  • Could you provide some more details and an example? Sounds like you need to switch window handle. Commented Mar 10, 2021 at 16:14

1 Answer 1

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

I was able to fix this issue with the code below, thanks. driver.switch_to.window(driver.window_handles[2])
This answer is very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.