水豚,通过ID和类检查HTML元素 [英] Capybara, checking HTML element by ID and Class

查看:89
本文介绍了水豚,通过ID和类检查HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个初学者的两个问题。

Two questions from a beginner.

Q1-是否可以通过ID和类来断言HTML节点的存在?
例如,查看是否存在以下元素:

Q1- Is it possible to assert the existence of an HTML node by ID and class? For example, to see if the following element exists:

<div class="drawer" id="first"....>

我见过您可以使用类似的东西:

I've seen you can use something like:

 page.should have_css('div.drawer')
 page.should have_css('div#first')

但是我们能否以某种方式查询两个参数的存在,我尝试了以下操作但没有起作用:

but can we somehow query for the existence of both parameters, I've tried the following and didn't work:

page.should have_selector("div", :class => "drawer", :id => "first")

Q2-是否可以在内部水豚方法中添加2个选择器,即,我看到您可以限制范围通过这样做:

Q2- Is it possible to add 2 selectors to the 'within' capybara method, ie, I've seen you can limit the scope by doing:

within("//div[@id='first']") do

但是我们可以通过添加id ='first'和class ='drawer'来过滤DIV吗?

but can we filter that DIV by adding id='first' and class='drawer' somehow?

非常感谢!

推荐答案

您可以组合选择器。

对于第一个问题,以下检查ID为 first且类为 drawer的div:

For your first question, the following checks for a div with id "first" and class "drawer":

page.should have_css('div#first.drawer')

对于第二个问题, within 块可以使用与上面相同的css-selector:

For your second question, the within block can use the same css-selector as above:

within('div#first.drawer') do

或者,如果您真的喜欢xpath,则可以:

Or if you really prefer xpath, you can do:

within("//div[@id='first' and @class='drawer']") do

css选择器的良好参考: http://www.w3.org/TR/CSS2/selector.html

A good reference for css-selectors: http://www.w3.org/TR/CSS2/selector.html

这篇关于水豚,通过ID和类检查HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆