Python webdriver 处理弹出的浏览器窗口,这不是警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17676036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:52:34  来源:igfitidea点击:

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

pythonseleniumwebdriver

提问by Ronu

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".

我正在开发一个 Web 应用程序,在其中单击某个链接会出现另一个弹出窗口。弹出窗口不是警报,而是一个包含各种字段的表单,由用户输入并单击“下一步”。

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

如何使用 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.
  1. 单击超链接(网址)-“单击此处”
  2. 用户注册表单显示为弹出窗口
  3. 数据由用户填写
  4. 单击下一步/提交按钮。
  5. 另一个下一个重定向到另一个页面/表单“用户个人信息页面”
  6. 个人信息由用户填写
  7. 点击“下一步/提交”
  8. 弹窗消失了。
  9. 现在在原始/基本页面上进一步处理。

回答by Mark Rowlands

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

如果它是一个新的window或一个,iframe你应该使用driver.switch_to_frame(webelement)or driver.switch_to_window(window_name)。这应该允许您与elements弹出窗口中的进行交互。完成后,您应该driver.switch_to_default_content()返回主网页。

回答by Lindsay

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.
  1. 很多人都知道的一个,就是driver.switch_to.window(window_handle)弹窗出现的时候需要同时使用,这样才能在弹窗中找到元素,和弹窗关闭后,才能在主窗口中找回元素。
  2. 只有机器慢的人才可能会遇到的情况是,当 Selenium 将窗口句柄作为变量提供时,它最初设置为 None,并需要一段时间才能填充一个值。

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

这里有一些代码可以在执行您请求的序列时解决这些问题。我省略了这些import语句,我使用了我希望很明显的变量名。另外,请注意我喜欢find_element(s)_by_xpath在我的代码中使用 ;随意使用其他find_element(s)_by方法:

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.

请让我知道是否有人(也许是我)需要在示例中添加更多内容,或提供其他信息,以使其更加清晰。