1

有了 watir,我可以获取特定脚本标签的 html

scripts = @browser.scripts
index = scripts.find_index {|s| s.html.include? 'mytest.string'}
icode = scripts[index].html
etc.

我正在寻找的字符串是唯一的,并且是较长字符串的一部分。我需要验证较长的字符串是否正确。现在我正在按照上述方法进行操作,但如果可能的话,我更愿意以页面对象的方式进行操作。我在其余代码中使用页面对象 gem 和 ruby​​。如果可能的话,想对这个脚本标签做同样的事情。如果不是,我会坚持使用 watir 方式。

一如既往地感谢您的帮助。

4

1 回答 1

0

我假设你的 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>"
于 2014-02-25T14:30:45.957 回答