regexrubyseleniumcapybaracapybara-webkit

How to find an element that doesn't match a specific text with Capybara?


With Capybara I am used to searching first('div', text: 'asdf'), but is there a way to search for elements that do not have the text?

I tried first('div', text: /^(?!asdf)/) and it still matches on div with 'asdf' text.

I checked to make sure my regex is right though: https://rubular.com/r/CN6MYKJNahiohD

Any ideas?


Solution

  • Not sure about Capybara, just guessing that you might be trying to design an expression that would exclude asdf with word boundaries in a string, maybe close to:

    ^(?!.*\basdf\b).*$
    

    The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

    Test

    re = /^(?!.*\basdf\b).*$/s
    str = 'some content before, then asdf as a word, some after
    some content before, then noasdf as a word, some after'
    
    str.scan(re) do |match|
        puts match.to_s
    end