简体   繁体   English

你如何使用 selenium 处理嵌套的 iframe/frame 标签?

[英]How do you handle nested iframe/frame tags using selenium?

So I finally figured out on how to handle a single iframe using所以我终于想出了如何使用处理单个 iframe

driver.switch_to.default_content()
driver.switch_to.frame("top")

and then turns out there is another iframe within this called "menu" (parent frame = "top" and child frame = "menu")然后发现在这个称为“菜单”的地方还有另一个 iframe(父框架 =“顶部”和子框架 =“菜单”)

I can't use the same code above because it goes all the way back我不能使用上面相同的代码,因为它一直追溯到

I need to perform one action in the parent frame and another one in the child frame我需要在父框架中执行一个动作,在子框架中执行另一个动作

how do i get this done ?我如何完成这项工作?

Switch again to the child from the parent frame再次从父框架切换到子框架

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

To handle multiple nested / you have to induce WebDriverwait twice for frame_to_be_available_and_switch_to_it() to switch through the parent and child frame as follows:要处理多个嵌套/ 您必须为frame_to_be_available_and_switch_to_it()引入WebDriverwait两次,以便按如下方式切换父frame和子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")))

Here you can find a relevant discussion on Ways to deal with #document under iframe在这里你可以找到关于Ways to deal with #document under iframe的相关讨论

Whenever dealing with nested frames, you have to find how many frames exist in the DOM element(From the developer console).每当处理嵌套框架时,您必须找出 DOM 元素中存在多少个框架(来自开发者控制台)。 The easiest way to keep dealing with nested frames are FILO - Simply its like the First_In_Last_out.处理嵌套帧的最简单方法是 FILO - 就像 First_In_Last_out 一样。 So once you loop into the frame that you want-Do necessary actions and then switch back frame by frame.所以一旦你循环到你想要的帧 - 做必要的动作,然后逐帧切换回来。

For instance......例如......

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

I hope this helps you我希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM