博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
web自动化测试第9步:切换页面frame
阅读量:4471 次
发布时间:2019-06-08

本文共 3548 字,大约阅读时间需要 11 分钟。

在实际的测试中,有时候我们会经常遇到找不到元素的问题,这也是自动化测试中经常会遇到的问题,这个时候我们可以考虑一下是否是frame的问题导致的定位不到元素。

一般frame也适用于:后台管理系统经常会遇到、页面中有一些独立的功能模块,特征是页面显示切换了模块,但是地址栏的URL并没有变化。下面我们来学习一下,如何通过切换frame来定位元素。

1.关于页面frame的方法

switch_to_frame(frame_reference):

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

switch_to_default_content():

切换到主页面

注:driver.switch_to_frame(None)等同于driver.switch_to_default_content()

2.frame的源码查看

webdriver.py

def switch_to_frame(self, frame_reference):    """ Deprecated use driver.switch_to.frame    """    warnings.warn("use driver.switch_to.frame instead", DeprecationWarning)    self._switch_to.frame(frame_reference)def switch_to_default_content(self):    """ Deprecated use driver.switch_to.default_content    """    warnings.warn("use driver.switch_to.default_content instead", DeprecationWarning)    self._switch_to.default_content()
switch_to.py

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})

通过观察源码,发现切换frame时,是通过id、name、索引、元素这四种方法来实现switch_to_frame()方法,而default_content的方法默认值为None,所以说我们可以发现switch_to_frame(None)等同于switch_to_default_content()

3.实例验证(1)w3c网站的frame切换

我们通过进入w3cschool网站的实验室例子来实验frame,这个层级关系是有一个父类的frame层级,里面有三个frame的子类层级

代码实例:

from selenium import  webdriver# 打开Chrome 进入示例页面driver = webdriver.Chrome()driver.get("http://www.w3school.com.cn/tiy/t.asp?f=html_frame_cols")# 定位父类层级iframeele_framest = driver.find_element_by_css_selector("#result > iframe")# 切换到父类层级iframe-通过元素切换driver.switch_to_frame(ele_framest)print(driver.page_source)print("----------------------------------------------------------------------------")# 切换到第二个子类frame-通过索引切换driver.switch_to_frame(1)print(driver.page_source)print("----------------------------------------------------------------------------")# 切换到最上层层级-等同于driver.switch_to_frame(None)driver.switch_to_default_content()print(driver.page_source)

4.实例验证(2)163邮箱首页登录

本来实例只有一个的,因为毕竟目前网站上用frame的不多,但是在一次别人问我的问题,163邮箱登录的问题,琢磨了一段时间,这里把问题展示一下:

163邮箱

我们如果直接定位输入框,会发现无法定位到元素,通过查看源代码发现,原来163把登陆的功能单独调过来,独立的作为一个模块,用frame来隔断了,所以说我们要做的就是先切换到对应的frame后,再去定位

代码演示:

from selenium import webdriverfrom time import sleepdriver=webdriver.Chrome()# 进入163邮箱首页driver.get("http://mail.163.com/")sleep(2)# 切换到包含登录框的frame下driver.switch_to_frame("x-URS-iframe")# 用id无法成功定位输入框# driver.find_element_by_css_selector("#auto-id-1501640728291").send_keys("123")# 通过name定位输入框,并输入数据driver.find_element_by_xpath("//input[@name='email']").send_keys("4444")
这里其实有一个问题:就是无法通过id来定位到输入框,这个问题我也不清楚为什么,希望能够在以后能够解开原因

转载于:https://www.cnblogs.com/CCGGAAG/p/7800328.html

你可能感兴趣的文章
ConstraintLayout布局介绍.md
查看>>
常用API接口汇总
查看>>
hive...
查看>>
计算机网络常见面试题(1)——TCP的三次握手和四次挥手
查看>>
Html form表单大全(一)
查看>>
客服系统引用方案
查看>>
如何在 Windows 上 使用 ONLYOFFICE 协作编辑文档
查看>>
python之numpy的基本使用
查看>>
分辨率和像素换算
查看>>
Linux CentOS 虚拟机下联网
查看>>
腾讯新闻编辑写出错别字,遭网友曝光
查看>>
python基础-循环语句while
查看>>
分享我的2014年3月unity3d面试题与参考答案(转)
查看>>
显示信息
查看>>
python 语句 笔记
查看>>
2018-2019 2 20165203 《网络对抗技术》 Exp1 PC平台逆向破解
查看>>
软件开发小组建议
查看>>
String定义与方法
查看>>
Linux-文件元数据操作
查看>>
Android中用Application类实现全局变量
查看>>