B Seven
B Seven

Reputation: 45943

How to use regex in a Capybara finder?

The following works great:

find "img[src='https://www.example.com/image']"

But I want to also find

find "img[src='https://www.example.com/image?foo=bar']"

How to use a regex within the attribute in the finder?

Upvotes: 11

Views: 8951

Answers (2)

sloneorzeszki
sloneorzeszki

Reputation: 1524

You can use a class option, as in find("div", class: /some-class/).

Upvotes: 1

Justin Ko
Justin Ko

Reputation: 46836

You can check that an attribute starts with a certain value using ^=:

find("img[src^='https://www.example.com/image']")

The article, The Skinny on CSS Attribute Selectors, describes the various checks (equals, starts with, ends with, etc.):

#Equals
find("img[src='https://www.example.com/image']")

#Contains somewhere
find("img[src*='https://www.example.com/image']")

#Begins with
find("img[src^='https://www.example.com/image']")

#Ends with
find("img[src$='https://www.example.com/image']")

Upvotes: 32

Related Questions