• web自动化测试第11步:切换窗口、frame、alert的新方法:switch_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")
    
    





  • 相关阅读:
    【XR-4】文本编辑器
    二十四、JMeter实战-Linux下搭建JMeter + Ant + Jenkins自动化框架
    Python 接口自动化
    Docker 部署 Tomcat
    CentOS7 查看 ip 地址
    Python
    Python接口自动化 -RESTful请求方法封装
    Python接口自动化
    Python
    xcode 编译webdriveragent
  • 原文地址:https://www.cnblogs.com/CCGGAAG/p/7800326.html
Copyright © 2020-2023  润新知