MJohnyJ
MJohnyJ

Reputation: 129

Capybara: Find element by attribute containing something NOT

I am having the following issue in my Capybara environment:

I need to search for an element. This element contains an attribute, which is unique. The attribute is changed asynchronously. I need the content of this attribute, but when I just search for the attribute I am getting a nil.

I could wait until the attribute has the property, but since this is Capybaras job, I thought there might be a possible selector, which can search for something like:

find('button[uniqueattribute] != nil')

Any ideas how to do this?

Thanks already!

Upvotes: 0

Views: 1721

Answers (2)

MJohnyJ
MJohnyJ

Reputation: 129

I found a possible solution: You can check the length of an element by Xpath (which is awesome) with string-length. So in my case the solution was:

find(:xpath, ".//button[@unique_attribute_name[string-length() != 0]]")

Now Capybara waits until the attribute has a value. If there are more pretty solutions, just tell me.

Upvotes: 0

Thomas Walpole
Thomas Walpole

Reputation: 49890

If the attribute is being added to the button element (didn't have the attribute originally) then you can just do

find('button[attribute_name]')

which will wait (up to Capybara.max_default_wait_time seconds for a button element with that attribute to be on the page - https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors. To wait and then get the contents/value of that attribute you could do

find('button[attribute_name]')['attribute_name']

If you just want to wait until the attribute is either not there or not blank then you can do

find('button:not([attribute_name=""])')

If you need to ensure the attribute is there, but isn't blank it would be

find('button[attribute_name]:not([attribute_name=""])')

Upvotes: 1

Related Questions