31

I am working on a web application, in which clicking on some link another popup windows appears. The pop windows is not an alert but its a form with various fields to be entered by user and click "Next".

How can I handle/automate this popup windows using selenium.

Summary :-

  1. Click on the hyperlink (url) - "CLICK HERE"
  2. A user registration form appears as a pop up
  3. A data is to be filled by user
  4. Click Next/Submit button.
  5. Another next redirected to another page/form 'User Personal Information Page'
  6. Personal information is to be filled by user
  7. Click "Next/Submit"
  8. Popup disappeared.
  9. Now further processing on original/Base page.
Ronu
  • 543
  • 1
  • 8
  • 17
  • If it's not an alert, there are a few different ways in which it could be a pop-up window. Can you include a screenshot of the web app, and the relevant bit of html? – Vince Bowdren Jul 18 '13 at 16:25
  • Could this be the root cause? A bypassed call to fxdriver.modals.clearFlag_ ... cf. https://stackoverflow.com/questions/44568402/how-do-i-manually-mouse-dismiss-a-javascript-alert-and-get-back-the-the-body-o/44592827#44592827 – NevilleDNZ Jun 16 '17 at 23:58

2 Answers2

45

Switching to a popup is challenging for at least two separate reasons:

  1. The one that many people know, which is that you need to use driver.switch_to.window(window_handle) both when the popup appears, so that you can find elements in the popup window, and after the popup is closed, so that you can find elements back in the main window.
  2. The one that only people with slow machines are likely to encounter, which is that when Selenium makes a window handle available as a variable, it's initially set to None, and takes a little while before it's filled in with a value.

Here's some code that addresses those issues while carrying out your requested sequence. I'm leaving out the import statements, and I'm using variable names that I hope are obvious. Also, note that I like to use find_element(s)_by_xpath in my code; feel free to use other find_element(s)_by methods:

main_window_handle = None
while not main_window_handle:
    main_window_handle = driver.current_window_handle
driver.find_element_by_xpath(u'//a[text()="click here"]').click()
signin_window_handle = None
while not signin_window_handle:
    for handle in driver.window_handles:
        if handle != main_window_handle:
            signin_window_handle = handle
            break
driver.switch_to.window(signin_window_handle)
driver.find_element_by_xpath(u'//input[@id="id_1"]').send_keys(user_text_1)
driver.find_element_by_xpath(u'//input[@value="OK"]').click()
driver.find_element_by_xpath(u'//input[@id="id_2"]').send_keys(user_text_2)
driver.find_element_by_xpath(u'//input[@value="OK"]').click()
driver.switch_to.window(main_window_handle) #or driver.switch_to_default_content()

Please let me know if someone (maybe me) needs to add more to the example, or provide other info, to make it more clear.

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Lindsay
  • 1,461
  • 16
  • 26
  • 2
    Your answer saved my life some time ago! It has worked like a charm, but since Chrome 54 and Chromedriver 2.25, there is no way to get the window handle of the extension popup. In `driver.window_handles` there are only the main window for me. Do you maybe have an other idea? Thanks! – monami Nov 29 '16 at 15:00
  • 1
    Better switch back to original window using `driver.switch_to_default_content()` as in @MarkRowlands answer https://stackoverflow.com/a/17676233/248616 – Nam G VU Jun 23 '17 at 09:19
  • 1
    In my case, there is no window handle created for the popup. My popup is a custom window and created with javascript. Is that the reason that no handle assigned to it? – Heinz Jan 08 '19 at 21:37
  • 2
    When I use driver.window_handles, it show me just one element while a popup page is available. Can you help me? – Hamed Baziyad May 19 '19 at 07:00
9

If its a new window or an iframe you should use the driver.switch_to_frame(webelement) or driver.switch_to_window(window_name). This should then allow you to interact with the elements within the popup. After you've finished, you should then driver.switch_to_default_content() to return to the main webpage.

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41