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 :-
Yes, it is possible to handle Windows based pop-ups in Selenium webdriver. Sometimes on clicking a link or a button, another window gets opened. It can be a pop up with information or an advertisement. The methods getWindowHandles and getWindowHandle are used to handle child windows.
You can run override script in the Selnium IDE before click on "Save" (or something like this) button through selenium. runScript - it should simple disable the confirmation window.
Switching to a popup is challenging for at least two separate reasons:
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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With