-2

I have a registration page that takes details from the user like contact details and address details.On click of register button, a bootstrap modal is opened which has buttons like close,try,etc. I want to access these buttons. I have tried switching handles but I am getting only one handle ie the main handle. The code for webdriver looks like this :

main_window_handle = driver.current_window_handle
print main_window_handle

driver.find_element_by_id("ctl00__mainContent_ucObjRegister_btnRegister").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
print main_window_handle
print signin_window_handle
driver.find_element_by_id("ctl00__mainContent_ucObjRegister_ucObjAddressVerification_btnCancel").click()

The btnCancel is present on the modal which doesn't get clicked.And the above loop goes into infinite loop.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
Shruti Agarwal
  • 887
  • 2
  • 14
  • 29
  • signin_window_handle = None while not signin_window_handle: these two lines are responsible for infinite loop.You need to remove this line : while not signin_window_handle: – Prakhar Trivedi Nov 29 '16 at 10:55
  • @PrakharTrivedi : Yeah I understood that its because of signin_Windw_handle. But signin_window_handle should get the value of the handle and break out of the loop which is not happening..it has only one handle – Shruti Agarwal Nov 29 '16 at 11:03
  • if handle != main_window_handle: this if condition never gets satisfied,so the access is not going inside it and then break is not happening. – Prakhar Trivedi Nov 29 '16 at 11:06
  • You need to change your code structure,no need for this line, while not signin_window_handle: the code will behave correctly after removing this line – Prakhar Trivedi Nov 29 '16 at 11:07
  • @PrakharTrivedi : The loop is the logic of the code. http://stackoverflow.com/questions/17676036/python-webdriver-to-handle-pop-up-browser-windows-which-is-not-an-alert – Shruti Agarwal Nov 29 '16 at 11:09
  • Updating your code,try this. – Prakhar Trivedi Nov 29 '16 at 11:21

2 Answers2

0

Try to reposition the break statement :

main_window_handle = driver.current_window_handle
print main_window_handle

driver.find_element_by_id("ctl00__mainContent_ucObjRegister_btnRegister").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
print main_window_handle
print signin_window_handle
driver.find_element_by_id("ctl00__mainContent_ucObjRegister_ucObjAddressVerification_btnCancel").click()

This way,even if the required if condition is not met,the loop will break after all the for loop iterations,and all the conditions will be checked as per the requirements.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
0

Finally after a lot of research over tonnes of website I could fix my issue. Basically the handle was not getting updated with modal handle because there is no new window created when the modal is made visible.The code for modal is present in your parent window itself just that it is hidden till you trigger it by some event.The elements on modal would be existing throughout your driver.It is the matter of visibility of the elements on modal.So explicitly waiting for the elements on modal to become visible solved my issue.Hope the answer helps :)

Shruti Agarwal
  • 887
  • 2
  • 14
  • 29