我假设你的 html 有类似的东西:
<html>
<body>
<script>other random script</script>
<script>some stuff mytest.string some other stuff</script>
<script>another random script</script>
</body>
</html>
要使用 page-object-gem 获取脚本元素,您可以使用通用element
访问器。然后,您可以将您的页面对象定义为:
class MyPage
include PageObject
element(:test_script, :script, :xpath => '//script[contains(text(), "mytest.string")]')
end
在这里,我使用 :xpath 定位器通过文本来定位脚本。使用 :xpath 通常不是建议的选择,但是我找不到其他解决方案。您不能使用 :text 定位器,因为 Watir 只返回可见文本,这对于脚本元素来说没有任何意义。:xpath 定位器允许您绕过该约束。
定义此页面后,您可以获取 script 元素的 html,如下所示:
page = MyPage.new(browser)
page.test_script_element.html
#=> "<script>some stuff mytest.string some other stuff</script>"