赞
踩
frame切换原理总结:
针对同一层级的frame,如果要进行切换的话,需要切回到默认的首页,不能同级子页之间切换
针对所要进入的frame,有多少个层级,就需要切换几次
不管当前在哪个层级,如果要回到默认首页,只需要调用一次回到默认首页的方法 driver.switch_to.default_content()
frame切换原理方法:
driver.switch_to.frame(reference)
driver.switch_to.parent_frame() # 切换到上一级frame
driver.switch_to.default_content() # 切换到默认frame
#获取当前iframe的tag name,确定有几个iframe,你所在的元素和获取到页面的iframe之间的关系,来进行iframe的切换。
for child_frame in driver.find_elements_by_tag_name("iframe"):
child_frame_id = child_frame.get_attribute("src")
print(child_frame_id)
def list_frame(self, locator): for child_frame in self.driver.find_elements(*locator): child_frame_id = child_frame.get_attribute("src") print(child_frame_id) # 切换到主frame def switch_body(self, locator, doc=''): logger.info('{0},body_frame切换到'.format(doc, locator)) try: self.driver.switch_to.parent_frame() except: logger.info('{0},body_frame切换到失败!!!'.format(doc, locator)) raise # 切换到不同的frame def switch_iframe(self, locator, doc=''): logger.info('{0},frame切换到'.format(doc, locator)) try: to_frame = self.get_element(locator) self.driver.switch_to.frame(to_frame) except: logger.info('{0},frame切换到失败!!!'.format(doc, locator)) raise #根据实际需求进行切换 # 切换到不同的frame def switch_iframe(self, locator='', doc='', relation="child"): logger.info('{0},frame切换到'.format(doc, locator)) try: if relation == "parent": self.driver.switch_to.default_content() else: to_frame = self.get_element(locator) self.driver.switch_to.frame(to_frame) except: logger.info('{0},frame切换到失败!!!'.format(doc, locator)) raise
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。