我正在使用Capybara和Ruby开发一个原子化测试。我需要在“报告”页面中切换框架以访问Web元素,但是我不知道该怎么做。
HTML代码:
<iframe name="OF_jreport" id="OF_jreport" width="100%" height="100%" frameborder="0" border="0"></iframe>
我正在尝试:
def check_supplier_report()
sleep 10
@session.switch_to_frame("//*[@id=\"OF_jreport\"]")
teste = @session.find("//*[@id=\"GTC_CODE\"]").text
puts teste
end
但是在控制台上,它返回以下错误:
调用switch_to_frame(ArgumentError)时必须提供frame元素,:parent或:top./features/helpers/commons.rb:158:in`check_supplier_report'
有人可以帮我吗?谢谢
基于错误消息,看来switch_to_frame
希望将帧元素作为参数传递。我相信您需要先找到框架,然后才能将其传递给此方法。
因此,将这两行替换为@session.switch_to_frame("//*[@id=\"OF_jreport\"]")
:
# Find the frame
frame = @session.find("//*[@id=\"OF_jreport\"]")
# Switch to the frame
@session.switch_to_frame(frame)
您应该尽可能地选择within_frame
而不是switch_to_frame
。 within_frame
较高级别,可确保系统保持稳定状态。您还应该考虑在可能的情况下更喜欢CSS而不是XPath,因为它可以更快更容易阅读。
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
[在可能的情况下,您应该更真正地选择in_frame而不是switch_to_frame,然后将其设置为in_frame('OF_jreport'){...在框架中执行任何操作}