当我设置一些简单的html
时<html>
<head>
<title>Simple Site</title>
</head>
<body>
<form>
<label>Name:</label><input type="text"></form><br/>
<label>Phone:</label><input type="text">
</form>
<div>happy</div>
<div>angry</div>
<div>sad</div>
</body>
</html>
我试图通过文字指定div但是不能
div[@text='happy'] #nope
html body div[@text='angry'] #nope
html body div:contains('angry') #nope
html body div[text='angry'] #nope
我错过了什么?
答案 0 :(得分:1)
CSS没有办法匹配元素的文本内容,但Capybaras find提供了text
选项来按文本内容进行过滤。需要字符串或正则表达式
angry_div = find(:css, 'div', text: 'angry') # The :css can be omitted if Capybara.default_selector = :css (which it is by default)
答案 1 :(得分:-1)
使用以下代码:
div[type='happy']{
background-color:red;
}
<form>
<label>Name:</label><input type="text"></form><br/>
<label>Phone:</label><input type="text">
</form>
<div type="happy">happy</div>
<div>angry</div>
<div>sad</div>