Capybara:按属性查找包含非

时间:2017-10-04 08:18:54

标签: selenium cucumber capybara

我在Capybara环境中遇到以下问题:

我需要搜索一个元素。此元素包含一个唯一的属性。该属性是异步更改的。 我需要这个属性的内容,但是当我只搜索属性时,我得到一个零。

我可以等到属性有属性,但由于这是Capybaras的工作,我认为可能有一个可能的选择器,它可以搜索类似的东西:

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

任何想法如何做到这一点?

谢谢!

2 个答案:

答案 0 :(得分:0)

如果要将属性添加到按钮元素(最初没有该属性),那么您可以这样做

find('button[attribute_name]')

将等待(对于具有该属性的按钮元素,将在Capybara.max_default_wait_time秒内显示在页面上 - https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors。要等待,然后获取该属性的内容/值,您可以执行

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

如果您只是想等到该属性不存在或不存在,那么您可以执行

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

如果您需要确保属性存在,但不是空白,则为

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

答案 1 :(得分:0)

我找到了一个可能的解决方案: 您可以使用字符串长度通过Xpath(非常棒)检查元素的长度。 所以在我的情况下,解决方案是:

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

现在Capybara会等到属性有值。如果有更好的解决方案,请告诉我。