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

编程入门 行业动态 更新时间:2024-10-27 14:33:05
本文介绍了Python webdriver 处理弹出的浏览器窗口,这不是警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我如何使用 selenium 处理/自动化这个弹出窗口.

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

总结:-

  • 点击超链接(网址)-点击这里"
  • 用户注册表单显示为弹出窗口
  • 用户填写数据
  • 点击下一步/提交按钮.
  • 另一个下一个重定向到另一个页面/表单用户个人信息页面"
  • 个人信息由用户填写
  • 点击下一步/提交"
  • 弹出窗口消失了.
  • 现在对原始/基本页面进行进一步处理.
  • 推荐答案

    切换到弹出窗口具有挑战性,至少有两个不同的原因:

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

  • 很多人都知道的一个,就是弹窗出现的时候需要同时使用driver.switch_to.window(window_handle),这样才能在弹窗中找到元素,以及弹出窗口关闭后,以便您可以在主窗口中找到元素.
  • 只有机器运行缓慢的人才可能会遇到的情况,即当 Selenium 将窗口句柄作为变量提供时,它最初设置为 None,并需要一段时间才能填充一个值.
  • 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.
  • 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.
  • 这里有一些代码可以在执行您请求的序列时解决这些问题.我省略了 import 语句,并使用了我希望显而易见的变量名.另外,请注意我喜欢在我的代码中使用 find_element(s)_by_xpath ;随意使用其他 find_element(s)_by 方法:

    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.

    更多推荐

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

    本文发布于:2023-11-27 23:02:12,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1639970.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:这不是   弹出   警报   浏览器   窗口

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!