Use a regexp instead of a string for the value of the :text
key:
find("a", :text => /\ABerlin\z/)
Check out the 'Options Hash' section of the Method: Capybara::Node::Finders#all
documentation.
PS: text matches are case sensitive. Your example code actually raises an error:
find("a", :text => "berlin")
# => Capybara::ElementNotFound:
# Unable to find css "a" with text "berlin"
Depending on which version of the gem you are using
find('a', text: 'Berlin', exact: true)
may be deprecated. In which case you would have to use
find('a', text: 'Berlin', match: :prefer_exact)
You can do so too:
find('a', text: 'Berlin', exact_text: true)
That will find for CSS.
And using only exact: true
instead of exact_text
will show you a msg that exact
option is only valid for XPATH.
Just use Capybara's exact option:
Capybara.exact = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With