水豚检查 HTML 类和 ID

发布于 2025-01-16 05:29:41 字数 417 浏览 1 评论 0原文

我目前正在尝试找到这个元素并让水豚验证它是否存在。有一个元素出现,我正在使用 page.should have_css 来查看该元素是否存在。

<i class="ui-grid-icon-cancel" ui-grid-one-bind-aria-label="aria.removeFilter" aria-label="Remove Filter">&nbsp;</i>

我目前正在尝试访问 ui-grid-icon-cancel 并验证它是否存在。这是我用来验证它的代码。

page.should have_css('class#ui-grid-icon-cancel')

我还能做什么来解决这个问题。

我期望使用水豚验证 CSS 元素。

I'm currently trying to find this element and have capybara validate that it's there. There's an element that shows up and I'm using page.should have_css to see if this element is there.

<i class="ui-grid-icon-cancel" ui-grid-one-bind-aria-label="aria.removeFilter" aria-label="Remove Filter"> </i>

I'm currently trying to get to the ui-grid-icon-cancel and validate that it's there. Here is the code I'm using to validate it.

page.should have_css('class#ui-grid-icon-cancel')

What else can I do to fix this.

I'm expecting to validate the CSS element using capybara.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生再见 2025-01-23 05:29:42

关于所需 CSS 选择器的所有其他答案都是正确的,但是如果您收到有关 should 未定义的错误,那么您可能没有正确安装 RSpec,或者您的项目具有 should 语法已禁用 - https://relishapp.com/rspec/rspec-expectations/docs/syntax-configuration。 RSpec 4 中默认会禁用 should 语法,此时任何新代码都不应该使用它。相反,您应该使用 expect 语法。假设您已正确安装 RSpec,以下任何操作都应该有效,具体取决于您要验证的内容

expect(page).to have_css('.ui-grid-icon-cancel')
expect(page).to have_css('i.ui-grid-icon-cancel')
expect(page).to have_css('i', class: 'ui-grid-icon-cancel')

All the other answers are correct about the needed CSS selectors, however if you're getting errors about should being undefined then you either haven't installed RSpec correctly or your project has the should syntax disabled - https://relishapp.com/rspec/rspec-expectations/docs/syntax-configuration. The should syntax is going to be disabled by default in RSpec 4, and any new code really shouldn't be using it at this point. Instead you should be using the expect syntax. Any of the following should work, assuming you have RSpec properly installed, depending on exactly what you're trying to verify

expect(page).to have_css('.ui-grid-icon-cancel')
expect(page).to have_css('i.ui-grid-icon-cancel')
expect(page).to have_css('i', class: 'ui-grid-icon-cancel')
乱了心跳 2025-01-23 05:29:42

您是否尝试过 page.should have_css('.ui-grid-icon-cancel') 。查找类元素时,您需要使用句点表示法,而不是明确地输入“类”一词。
`

Did you try page.should have_css('.ui-grid-icon-cancel') . When looking for a class element, you need to use the notation of a period rather then explicitly putting the word "class".
`

百合的盛世恋 2025-01-23 05:29:42

您可以使用以下任一定位器策略

  • css_selector 使用 class 属性的值:

    page.should have_css('i.ui-grid-icon-cancel')
                           ^ 这里的点表示类名
    
  • css_selector 使用 aria-label 属性的值:

    page.should have_css('i[aria-label="删除过滤器"]')
    

You can use either of the following locator strategies:

  • css_selector using value of class attribute:

    page.should have_css('i.ui-grid-icon-cancel')
                           ^ here dot denotes classname
    
  • css_selector using value of aria-label attribute:

    page.should have_css('i[aria-label="Remove Filter"]')
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文