当前位置:   article > 正文

selenium切换frame_selenium frame切换

selenium frame切换

Frame标签框架区别

  • Frameset:可以直接按照正常元素定位
  • Frame:需要把驱动切换到Frame内再进行操作
  • IFrame:需要把驱动切换到Frame内再进行操作

切换总结

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
  • 1
  • 2
  • 3

在这里插入图片描述

selenium定位当前处于那个iframe(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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

frame封装

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/804936
推荐阅读
相关标签
  

闽ICP备14008679号