【问题标题】:Look for text in a div that has multiple links under it with Watir使用 Watir 在具有多个链接的 div 中查找文本
【发布时间】:2013-05-13 04:02:08
【问题描述】:

我正在尝试验证链接的文本是否存在于 .在单击链接之前,该 div 不可用。点击链接后,更多的链接可以点击。以下是单击以显示其他链接后 html 的样子。

<div class="more-links">
   <a href="url">First Link</a>
   <a href="url">Second Link</a>
   <a href="url">Third Link</a>
   <a href="url">Fourth Link</a>
   <a href="url">Fifth Link</a>
</div>

我想确保在 div 出现后出现“第三链接”文本。

我试过了,但是 watir 说它找不到那个元素。

assert_equal(true, browser.div(:class => "more-links").a(:text => "Third Link"))

我可以用这个找到文本,但速度很慢,想指定这个特定的 div

browser.text.include?("Third Link")

想法?

【问题讨论】:

  • 如果你用的是cucumber,你为什么用assert而不是.should?
  • browser.div(:class =&gt; "more-links").a(:text =&gt; "Third Link").should exist

标签: ruby testing automation cucumber watir


【解决方案1】:

以下代码不会返回true

browser.div(:class => "more-links").a(:text => "Third Link")
#=> #<Watir::Link:0x..f99c8864c located=false specifiers={:tag_name=>["a"], :text=>"Third Link"}>

所以,assert_equal 返回 false。

如果exists? 附加到该行,则返回true

browser.div(:class => "more-links").a(:text => "Third Link").exists?
#=> true

【讨论】:

  • #a 实际上为我返回了一个 Watir::Anchor 对象(不同的 watir-webdriver 版本?),但无论如何它肯定不是布尔值。
  • 好点。我使用的是 watir-classic,它返回 Watir::Link,而不是 watir-webdriver 中的 Watir::Anchor 对象。
  • 同名的类不止一个,我想这就是为什么 watir 找不到元素的原因。谢谢
  • 你的意思是这个类有多个 Div 吗?在这种情况下,您将需要添加一个额外的限定符,例如索引,或者从一个包含 div 的唯一容器开始。在这种情况下,向我们展示更多的 HTML 可能会有所帮助
【解决方案2】:

browser.text.include?("第三链接")

我可以用这个找到文本,但速度很慢,想指定 这个特殊的 div

...这正是你在这里所做的:

browser.div(:class => "more-links").a(:text => "Third Link")

因此,在您的原始代码行中,您只需将“浏览器”替换为您更具体的搜索:

browser.div(:class => "more-links").a(:text => "Third Link")

还要注意,你永远不会写:

assert_equal(true, ...)

因为您可以节省一些打字时间,只需编写:

assert(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2015-05-17
    • 2011-07-04
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多