如何使用 Capybara 和 Ruby 切换 Frame?
How can I switch Frame using Capybara and Ruby?
我正在使用 Capybara 和 Ruby 开发原子化测试。我需要切换框架以访问报告页面中的网络元素,但我不知道该怎么做。
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
但在控制台上它是 return 以下错误:
You must provide a frame element, :parent, or :top when calling switch_to_frame (ArgumentError)
./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
你真的应该更喜欢 within_frame 而不是 switch_to_frame 只要有可能就会 within_frame('OF_jreport') { ... 在框架中做任何事情}
我正在使用 Capybara 和 Ruby 开发原子化测试。我需要切换框架以访问报告页面中的网络元素,但我不知道该怎么做。
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
但在控制台上它是 return 以下错误:
You must provide a frame element, :parent, or :top when calling switch_to_frame (ArgumentError) ./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
你真的应该更喜欢 within_frame 而不是 switch_to_frame 只要有可能就会 within_frame('OF_jreport') { ... 在框架中做任何事情}