三次iframe框架切换

记录一次坑。
做UI自动化,页面是嵌套的frame框架, 整个页面是一个iframe。在iframe里面,上方是一个frame,下方是一个frame。下方frame里又分为左右两个frame。所以要定位右侧页面元素,需要三次切入frame框架。
在basepage里有如下代码:
#切三层进入下方右侧frame
def switch_right_frame(self):
# 定位iframe框架
iframe1 = self.locate_element(’//*[@id=“form1”]/div[3]/iframe’)
# 切换进iframe
self.driver.switch_to.frame(iframe1)

    # 定位下方iframe
    frame2 = self.locate_element('/html/frameset/frame[2]')
    self.driver.switch_to.frame(frame2)
    frame_right = self.locate_element('//*[@id="content"]/frame[2]')
    self.driver.switch_to.frame(frame_right)

运行提示定位不到frame2
在没有写函数的时候,是可以成功定位到右侧元素的。所以找不到原因。

尝试解决方法:
1.在自己定义的locate_element函数里加上显示等待。
#根据元素类型进行元素定位
#显示等待元素存在
def locate_element(self,locator,type=‘xpath’):
if type==‘xpath’:
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH,locator)))
self.driver.find_element_by_xpath(locator)

结果运行超时也没有定位到。

2.猜测是没有切入到iframe框架里。
问题在这行代码里
iframe1 = self.locate_element(’//*[@id=“form1”]/div[3]/iframe’)
我用变量去接收定位到的元素,但是locate_element函数没有return,其实这个变量什么也没接收到,所以根本没有切入iframe。
改代码:
#根据元素类型进行元素定位
#显示等待元素存在
def locate_element(self,locator,type=‘xpath’):
if type==‘xpath’:
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH,locator)))
return self.driver.find_element_by_xpath(locator)
定位成功,用例通过。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值