简体   繁体   English

如何使用watir将焦点从iframe更改回主页?

[英]How can I change the focus from an iframe back to main page using watir?

I have a nested iframe that looks something like this 我有一个嵌套的iframe,看起来像这样

<iframe class="first">
   <button></button>
   <iframe class="second">
      <button></button>
   </iframe>
</iframe>

I switched into the second iframe to work with elements within that iframe, and now I want to go back and work with elements in the first iframe. 我切换到第二个iframe以使用该iframe中的元素,现在我想返回并使用第一个iframe中的元素。 But I can't get out of the iframe using watir for some reason... 但是由于某些原因,我无法使用watir离开iframe ...

I've tried using $b.frame(index: 0).locate - I get an "unable to locate frame" error 我尝试使用$b.frame(index: 0).locate我收到“无法找到框架”错误

I've also tried doing $b.iframe(class: "first").locate , but that didn't work either 我也尝试做$b.iframe(class: "first").locate ,但是那也不起作用

Please help! 请帮忙!

Watir handles browser context switching for you. Watir为您处理浏览器上下文切换。 You only need to include the full iframe path in the definition of the element. 您只需要在元素的定义中包括完整的iframe路径即可。

Watir lazy loads so this doesn't actually locate them, just stores their location information Watir延迟加载,因此实际上并没有找到它们,只是存储了它们的位置信息

b1 = browser.iframe(class: 'first').button
b2 = browser.iframe(class: 'first').iframe(class: 'second').button

To click the second button, Watir will automatically switch into first frame context, then the second frame context, then click that button 要单击第二个按钮,Watir将自动切换到第一个框架上下文,然后切换到第二个框架上下文,然后单击该按钮

b2.click

Watir will automatically switch between any browsing contexts without needing to be explicitly specified: Watir将自动在任何浏览上下文之间切换,而无需明确指定:

b1.click
b2.click
b1.click

You shouldn't need to explicitly change contexts, just interact with the next element you need to use, but either of these will get you back to the top level browsing context: 您不需要显式更改上下文,只需与您需要使用的下一个元素进行交互,但是其中任何一个都会使您回到顶级浏览上下文:

browser.exist?
browser.iframe.exist?

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

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