自动化测试——多窗口切换和切换frame

在人生的道路上,不管是潇洒走一回,或者是千山独行,皆须是自己想走的路,虽然,有的人并不是很快就能找到自己的方向和道路,不过,只要坚持到底,我相信,就一定可以找到自己的路,只要找到路,就不必怕路途遥远了。

导读:本篇文章讲解 自动化测试——多窗口切换和切换frame,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

在这里插入图片描述


一、多窗口切换

获取当前的窗口句柄:driver.current_window_handles
所有的窗口句柄:driver.window_handles
切换窗口:driver.switch_to.window()

1、base.py:公共代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/2/23 11:18
# @Author  : 杜兰特
# @File    : base.py

import sys
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

class Base:
    def setup_class(self):
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(3)
        self.driver.maximize_window()

    def teardown(self):
        self.driver.quit()

继承Base仍然先执行setup_class
最后执行test_down()

2、切换句柄的方式1,通过for循环

for item in window_handles:
    if item != self.driver.current_window_handle:
        self.driver.switch_to.window(item)

3、切换句柄的方式2,通过索引切换

self.driver.switch_to.window(window_handles[-1])

4、源代码

import sys
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from .base import Base

class TestSwitchWindow(Base):
    #继承Base仍然先执行setup_class
    #最后执行test_down()
    def test_switch_window(self):
        self.driver.get('http://www.baidu.com')
        self.driver.find_element(By.XPATH,'//a[text()="新闻"]').click()
        window_handles=self.driver.window_handles
		
		#切换到最新的窗口
        self.driver.switch_to.window(window_handles[-1])
        time.sleep(5)
		
		#按照坐标的形式,滑动到指定的位置
        ActionChains(self.driver).scroll_by_amount(0,3000).perform()
        time.sleep(3)
        
		#再切换到最新的窗口
        self.driver.switch_to.window(window_handles[0])
        time.sleep(3)
        
        #文本框中输入文字666
        self.driver.find_element(By.CSS_SELECTOR,'.s_ipt').send_keys('666')
        time.sleep(3)

二、frame窗口

在web自动化中,如果一个元素定位不到,那么很大可能是在iframe中。

1、什么是frame?

frame是html中的框架,在html中,所谓的框架就是可以在同一个浏览器中显示不止一个页面。
基于html的框架,又分为垂直框架和水平框架

2、Frame 分类

frame标签包含frameset、frame、iframe三种,
frameset和普通的标签一样,不会影响正常的定位,可以使用index、id、name、webelement任意种方式定位
frame。
而frame与iframe对selenium定位而言是一样的。selenium有一组方法对frame进行操作

frame存在两种:一种是嵌套的,一种是非嵌套的。

3、判断要定位的元素在不在frame中两种方式

方式一:鼠标选中要定位的元素,底部如果能看到iframe的字样,说明在frame中

在这里插入图片描述

方式二:鼠标选中要定位的元素,向上查是否存在iframe标签

在这里插入图片描述

4、切换未嵌套的iframe

driver.switch_to.frame(‘frame的id’):按照frame中的id属性切换frame
driver.switch_to_frame(‘frame-index’):frame无ID的时候依据索引来处理,
索引从0开始driver.switch_to_frame(0)

a、使用iframe中的id属性来切换frame

import sys
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from .base import Base

class TestSwitchWindow(Base):
    #继承Base仍然先执行setup_class
    #最后执行test_down()
    
    def test_switch_frame(self):
        self.driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
        ele=self.driver.find_element(By.ID,'iframeResult')
        self.driver.switch_to.frame(ele)
        self.driver.find_element(By.XPATH,'//div[text()="请拖拽我!"]')
        self.driver.switch_to.parent_frame()
        self.driver.find_element(By.XPATH,'//button[contains(text(),"点击运行")]')
        time.sleep(3)

b、依据索引来切换frame

import sys
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from .base import Base

class TestSwitchWindow(Base):
    #继承Base仍然先执行setup_class
    #最后执行test_down()
    
    def test_switch_frame1(self):
        self.driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
        self.driver.switch_to.frame(0)
        self.driver.find_element(By.XPATH,'//div[text()="请拖拽我!"]')
        self.driver.switch_to.parent_frame()
        self.driver.find_element(By.XPATH,'//button[contains(text(),"点击运行")]')
        time.sleep(3)

5、切换嵌套的iframe

对于嵌套的先进入到iframe的父节点,再进入子节点,然后可以对子节点里面的对象进行处理和操作
driver.switch_to.frame(‘父节点’)
driver.switch_to.frame(‘子节点’)

在一个iframe中,如果还嵌套了另一个iframe
进入:一个一个的进,先进第一层的iframe,一层一层的进
如果出呢:一层一层的出

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123129.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!