记录一次坑。
做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)
定位成功,用例通过。