我需要使用 capyvara 从所选单选按钮内的 span 标签中读取文本值
我有一个 radio button
列表,后面是文本及其在括号中的计数。
例如:带有 Thank You(82)
的单选按钮
我想要的是读取括号内选定的单选按钮计数 82。
我使用了下面的代码..但是它不起作用
value=page.find(".cardFilterItemSelection[checked='checked'] + span.itemCount").text
并尝试使用 Xpath 但没有得到任何东西
value=page.find(:xpath,"//input[@class = 'cardFilterItemSelection' and @checked = 'checked']/span[@class = 'itemCount']/text()")
这怎么可能?
<label id="thankyou_label" for="thankyou_radio" class="itemName radio">
<input checked="checked" tagtype="Occasion" value="Thank You" id="thankyou_radio" name="occasionGroup" class="cardFilterItemSelection" type="radio">
<span class="occasion_display_name">
Thank You
</span>
<span class="itemCount">
(82)
</span>
</label>
<label id="spring_label" class="itemName radio" for="spring_radio">
<input id="spring_radio" class="cardFilterItemSelection" type="radio" name="occasionGroup" value="Spring" tagtype="Occasion">
<span class="occasion_display_name">
Spring
</span>
<span class="itemCount">
(0)
</span>
</label>
最佳答案
我认为您走在正确的轨道上。但是您应该查询的不是跨度包含的文本节点,而是跨度本身并使用 XPath axes
span = page.find(:xpath,"//input[@class = 'cardFilterItemSelection' and @checked = 'checked']/following-sibling::span[@class = 'itemCount']")
value = span.text
不过我个人觉得 css 选择器更具可读性(除非你需要进行复杂的查询)
span = page.find(:css, 'input.cardFilterItemSelection[checked=checked] ~ span.itemCount')
value = span.text
关于ruby - 从 span 标签中读取文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17941474/