如何使用Capybara和Ruby切换帧?

如何使用Capybara和Ruby切换帧?,ruby,selenium,automated-tests,capybara,Ruby,Selenium,Automated Tests,Capybara,我正在用水豚和Ruby开发一个原子化测试。我需要切换框架来访问报告页面中的webelement,但我不知道如何才能做到这一点 HTML代码: <iframe name="OF_jreport" id="OF_jreport" width="100%" height="100%" frameborder="0" border="0"></iframe> 但在控制台上,返回以下错误: 调用switch\u to\u frame(ArgumentError)时,必须提供帧元素

我正在用水豚和Ruby开发一个原子化测试。我需要切换框架来访问报告页面中的webelement,但我不知道如何才能做到这一点

HTML代码:

<iframe name="OF_jreport" id="OF_jreport" width="100%" height="100%" frameborder="0" border="0"></iframe>
但在控制台上,返回以下错误:

调用switch\u to\u frame(ArgumentError)时,必须提供帧元素:parent或:top /features/helpers/commons.rb:158:在“检查供应商报告”中


有人能帮我吗?谢谢

根据错误消息,似乎
switch_to_frame
希望将frame元素作为参数传递。我相信你需要先找到框架,然后才能将其传递到这个方法中

因此,用这两行代码替换这一行
@session.switch\u到\u frame(“//*[@id=\”OF \u jreport\”])

# Find the frame
frame = @session.find("//*[@id=\"OF_jreport\"]")

# Switch to the frame
@session.switch_to_frame(frame)

只要有可能,您应该选择
在_框架内
而不是
将_切换到_框架
在_框架内
是更高的级别,确保系统处于稳定状态。 在可能的情况下,您也应该考虑优先选择CSS,因为它读起来更快更简单。
def check_supplier_report()
  sleep 10 # ??? Not sure why you have this

  @session.within_frame("OF_jreport") do
     teste = @session.find(:css, "#GTC_CODE").text
     puts teste
  end
end
如果可能的话,您应该更喜欢在_帧内而不是将_切换到_帧,这将在_帧内('OF _jreport'){…在帧中做任何事情}

def check_supplier_report()
  sleep 10 # ??? Not sure why you have this

  @session.within_frame("OF_jreport") do
     teste = @session.find(:css, "#GTC_CODE").text
     puts teste
  end
end