Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

selenium - Python webdriver to handle pop up browser windows which is not an alert

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...