selenium中使用显示等待判断并切换window、iframe、alert源码解析

本文详细介绍了如何使用Selenium的显示等待来判断并切换窗口、iframe和alert。重点讲解了new_window_is_opened()用于检查新窗口是否打开,frame_to_be_available_and_switch_to_it()用于iframe的存在判断与切换,以及alert_is_present()用于处理alert的出现。通过示例代码展示了具体操作方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、简介

本文只针对用显示等待去判断window、iframe、alert是否存在,其中
window用的是new_window_is_opene()这个类,真判断是否存在,不切换
iframe用的是frame_to_be_available_and_switch_to_it()这个类,判断是否存在,存在返回True并切换
alert用的是alert_is_present()这个类,判断是否存在,存在返回True并切换,反之返回False

二、显示等待及判断导包

from selenium.webdriver.support.wait import WebDriverWait # 导入显示等待模块
from selenium.webdriver.support import expected_conditions as ec  # 导入显示等待中判断的条件

三、window切换

用法
其中current_handles参数为在窗口变化之前的所有窗口总数
WebDriverWait(driver,10).until(ec.new_window_is_opened(current_handles))


**先来看下源码**
  class new_window_is_opened(object):
    """ An expectation that a new window will be opened and have the number of
    windows handles increase"""

    def __init__(self, current_handles):
        self.current_handles = current_handles

    def __call__(self, driver):
        return len(driver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值