web自动化测试第11步:切换窗口、frame、alert的新方法:switch_to包详解

摘要:
在前三节中,我们详细解释了窗口切换、帧切换和弹出窗口切换。但当我们编写代码时,我们发现这些方法被编辑器抓伤了,但这些方法仍然可以正常使用。然而,当前的pycharm不建议您继续以这种方式使用它们。如果我们不使用这些方法,我们如何完成窗口切换?帧操作呢?因此,让我们学习替换这些方法的开关_to包。

在之前的三节里,我们分别对窗口切换(handle)、frame切换、弹窗(alert)切换做了详细的解释,但是我们在写代码的时候发现,这些方法都被编辑器划伤了一条横线,但是方法还是可以正常使用,只是目前的pycharm不推荐你继续这样使用了(有新的方法可以替代它),那如果我们不使用这些方法的话,我们该怎么去完成切换窗口、frame这些操作呢?所以我们来学习一下替代这几个方法的switch_to包。

不被推荐的方法

1.switch_to包的方法详解

在switch_to的基础上,有这么几个方法,鉴于基本上都是之前曾经讲过的,这次把等价的方法也列出来,供大家参考

driver.switch_to.active_element()                                         等同于    driver.switch_to_active_element()

定位到当前聚焦的元素上

driver.switch_to.alert()                                                          等同于    driver.switch_to_alert()

切换到alert弹窗

driver.switch_to.default_content()                                    等同于    driver.switch_to_default_content()

切换到最上层页面

driver.switch_to.frame(frame_reference)                       等同于    driver.switch_to_frame(frame_reference)

通过id、name、element(定位的某个元素)、索引来切换到某个frame

driver.switch_to.parent_frame()

这是switch_to中独有的方法,可以切换到上一层的frame,对于层层嵌套的frame很有用

driver.switch_to.window(window_name)等同于 driver.switch_to_window(window_name)

切换到制定的window_name页面

注: 官方把selenium.webdriver包中的switch方法全部封装成了一个包,这样能够比较明了和方便,也符合软件编程中的高内聚低耦合的思想。

2.源码展示

class SwitchTo:
    def __init__(self, driver):
        self._driver = driver

    @property
    def active_element(self):
        """
        Returns the element with focus, or BODY if nothing has focus.

        :Usage:
            element = driver.switch_to.active_element
        """
        if self._driver.w3c:
            return self._driver.execute(Command.W3C_GET_ACTIVE_ELEMENT)
        else:
            return self._driver.execute(Command.GET_ACTIVE_ELEMENT)['value']

    @property
    def alert(self):
        """
        Switches focus to an alert on the page.

        :Usage:
            alert = driver.switch_to.alert
        """
        return Alert(self._driver)

    def default_content(self):
        """
        Switch focus to the default frame.

        :Usage:
            driver.switch_to.default_content()
        """
        self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})

    def frame(self, frame_reference):
        """
        Switches focus to the specified frame, by index, name, or webelement.

        :Args:
         - frame_reference: The name of the window to switch to, an integer representing the index,
                            or a webelement that is an (i)frame to switch to.

        :Usage:
            driver.switch_to.frame('frame_name')
            driver.switch_to.frame(1)
            driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
        """
        if isinstance(frame_reference, basestring) and self._driver.w3c:
            try:
                frame_reference = self._driver.find_element(By.ID, frame_reference)
            except NoSuchElementException:
                try:
                    frame_reference = self._driver.find_element(By.NAME, frame_reference)
                except NoSuchElementException:
                    raise NoSuchFrameException(frame_reference)

        self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})

    def parent_frame(self):
        """
        Switches focus to the parent context. If the current context is the top
        level browsing context, the context remains unchanged.

        :Usage:
            driver.switch_to.parent_frame()
        """
        self._driver.execute(Command.SWITCH_TO_PARENT_FRAME)

    def window(self, window_name):
        """
        Switches focus to the specified window.

        :Args:
         - window_name: The name or window handle of the window to switch to.

        :Usage:
            driver.switch_to.window('main')
        """
        data = {'name': window_name}
        if self._driver.w3c:
            data = {'handle': window_name}
        self._driver.execute(Command.SWITCH_TO_WINDOW, data)

3.实际案例展示

这节课我们再来把163邮箱登录的例子来用新的switch_to方法写一下,并通过观察,我们发现进入这个页面后焦点直接就定位到输入框里了,所以我们可以通过active_element()来定位。

from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()

# 进入163邮箱首页
driver.get("http://mail.163.com/")
sleep(2)

# 切换到包含登录框的frame下
driver.switch_to.frame("x-URS-iframe")

# 通过定位输当前焦点元素,并再次输入数据
driver.switch_to.active_element.send_Keys("123")






免责声明:文章转载自《web自动化测试第11步:切换窗口、frame、alert的新方法:switch_to包详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇多线程中如何取消任务oracle中的exists 和not exists 用法详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

HBuilder html5plus分享功能

1》使用系统分享功能,这个分享经测试(202006月)只能分享文字,不能分享图片。毕竟这不需要任何appid,还可以接受。 if(window.plus){ plusReady(); }else{ document.addEventListener("plusready", plusReady, false);...

alertmanager

alertmanager主要用于接收prometheus发送的告警信息; wget下载,解压, 配置alertmanager.yml,内容如下; 在prometheus文件下添加rules.yml内容如下: groups: - name: test-rules rules: - alert: InstanceDown expr: up == 0 for:...

iOS 和 Android 中的Alert

iOS 和 Android中都有alert这种提示框,下面简单介绍下。 ios中的alert叫做UIAlertView,共有4种样式,由于在ios7上,自定义alertview不太好用,所以也就这4种样式。 typedef enum { UIAlertViewStyleDefault = 0, //不带输入框 UIAlertViewSt...

web常用自动化库——selenium总结

概述 selenium是一个模拟控制浏览器操作的自动化库,它可以做到元素定位,鼠标事件,浏览器事件,js脚本执行等操作 与request不同的是,request是单独请求一个http,而selenium则是以浏览器方式加载整个页面流,所以那些异步的请求也不用像request一样去分析接口,一切都像浏览器一样,所见即所得。 优点是: 1.所见即所得,不用考虑...

JavaScript学习笔记及知识点整理_1

一.js的基础部分 1.==和===的区别:==在判断是否相等的时候会进行类型转换,有时会得到非常奇怪的结果,因此一般情况下都是用===判断是否相等2.strict模式:在js中,如果一个变量没有用var进行声明,那么这个变量将会变成全局变量.采用strict模式声明后,如果该变量没有用var声明,那么将会报错!采用strict模式的方法是在js的第一行加...

Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载

元素定位及其他操作 0、常见的识别元素的方法是什么? 1 driver.find_element_by_id() 2 driver.find_element_by_name() 3 driver.find_element_by_class_name() 4 driver.find_element_by_tag_name() 5 driver.find...