【问题标题】:How do you handle nested iframe/frame tags using selenium?你如何使用 selenium 处理嵌套的 iframe/frame 标签?
【发布时间】:2019-12-22 13:54:06
【问题描述】:

所以我终于想出了如何使用

处理单个 iframe
driver.switch_to.default_content()
driver.switch_to.frame("top")

然后发现其中还有另一个 iframe,称为“菜单”(父框架 =“顶部”,子框架 =“菜单”)

我不能使用上面相同的代码,因为它会一直返回

我需要在父框架中执行一项操作,在子框架中执行另一项操作

我该怎么做?

【问题讨论】:

    标签: python selenium iframe frame


    【解决方案1】:

    再次从父框架切换到子框架

    driver.switch_to.frame("top")
    # do something
    driver.switch_to.frame("menu")
    # do something
    driver.switch_to.default_content()
    

    【讨论】:

      【解决方案2】:

      要处理多个嵌套的 / ,您必须诱导 WebDriverwait 两次,以便 frame_to_be_available_and_switch_to_it() 在父子 frame 之间切换,如下所示:

      driver.switch_to.default_content()
      # SwitchTo parent frame = "top"
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"parent_iframe_css")))
      # SwitchTo child frame = "menu"
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"child_iframe_css")))
      

      这里可以找到Ways to deal with #document under iframe的相关讨论

      【讨论】:

        【解决方案3】:

        在处理嵌套框架时,您必须找出 DOM 元素中存在多少框架(来自开发者控制台)。 处理嵌套帧的最简单方法是 FILO - 就像 First_In_Last_out 一样。因此,一旦您循环到您想要的帧 - 执行必要的操作,然后逐帧切换回来。

        比如……

        driver.switch_to.frame("frame_1")
        # All necessary actions
        driver.switch_to.frame("frame_2")
        # All necessary actions
        driver.switch_to.default_content()
        

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-19
          • 2021-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-07
          • 1970-01-01
          相关资源
          最近更新 更多