我试图在Capybara找到方法并且没有太多运气。我希望第二个发现只在第一个查找的输出中查找文本。但是,第二个查找搜索整个文档。
element = page_foo.find('.bar')
element.find('div', :text => "name")
=> All divs with the text "name" on the page.
这里描述了使用xpath的解决方案:
capybara - Find with xPath is leaving the within scope
但是,我对xpath并不太熟悉,希望能找到解决它的方法。
其他详情:
HTML:
<div data-id="tabMeListProducts">
<div class=" title">My BMW 5-SERIES GRAN TURISMO.</div>
</div>
<div data-id="tabMeListPosts">
<div class="name">My BMW 5-SERIES GRAN TURISMO.</div>
</div>
RSPEC:
element = page_foo.find('[data-id="tabMeListProducts"]')
=>#<Capybara::Element tag="div">
element.find('div', :text => "My BMW 5-SERIES GRAN TURISMO.")
=>Capybara::Ambiguous: Ambiguous match, found 2 elements matching css "div" with text "My BMW 5-SERIES GRAN TURISMO."
从上面的示例中可以看出,第二个查找范围是整个页面,并且不包含在初始查找传入的元素中。
我希望find只能查看变量&#34; element&#34;中包含的div。
答案 0 :(得分:3)
I remember nested find calls working in past versions of Cucumber or Capybara. However, things change with every version. Regardless, your example is more easily handled with nested CSS expressions instead of relying on multiple finds.
For example:
val s = "\\N"
println(s.matches("\\\\N")) // prints true
or
page_foo.find('[data-id="tabMeListProducts"] .title')
If there are multiple occurrences, I would use page_foo.find('[data-id="tabMeListProducts"] div', :text => "My BMW 5-SERIES GRAN TURISMO.")
instead of all
and run my own Ruby checking logic on the collection.
Also, find
usually works as an alternative (no need to call within
with it as one response mentioned, you can add CSS directly next to it):
find
Lastly, if you insist on using nested find calls, try doing them on the same Ruby line as mentioned by the Capybara documentation:
within '[data-id="tabMeListProducts"]' do
element.find('div', :text => "My BMW 5-SERIES GRAN TURISMO.")
end
It's possible it's lazy fetched, but activates at the end of a Ruby line (through meta-programming checks for line number).
If all else fails, open a GitHub issue: http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Finders#find-instance_method
You'd be surprised by how fast people respond to you, but I have a feeling one of the solutions above will just work.
Good luck! I understand this can be a frustrating issue, but I'm sure you got it!
答案 1 :(得分:1)
我同意@Taryn,你需要在syntex中使用。即
within(".bar")do
find('div', :text => "name")
end