我会有HTML
<div class="checklistbox HOTELS" name="HOTELS">
<label><input type="checkbox" value="1" filters="" />Cat</label>
<label><input type="checkbox" value="2" filters="" />Dog</label>
<label><input type="checkbox" value="3" filters="" />Pony</label>
<label><input type="checkbox" value="4" filters="" />black cat</label>
</div>
如果标签包含未选中的复选框,我如何从标签中获取文字?
browser.checkboxes(:xpath => '//*[@id="HOTELSCONTAINER"]/td[4]/div[2]/label/input').each do |l|
if l.set? == false
-- Get a label text --
end
end
有答案:
@i = 0
loop do
@i = @i + 1
if (browser.checkbox(:xpath => '//*[@id="HOTELSCONTAINER"]/td[4]/div[2]/label[' + @i.to_s + ']/input').exists?)
if browser.checkbox(:xpath => '//*[@id="HOTELSCONTAINER"]/td[4]/div[2]/label[' + @i.to_s + ']/input').set? == false
puts browser.element(:xpath => '//*[@id="HOTELSCONTAINER"]/td[4]/div[2]/label[' + @i.to_s + ']').text
end
else
break
end
end
答案 0 :(得分:0)
你可以:
一种方法是使用一系列拒绝/地图:
labels = browser.div(class: 'HOTELS').checkboxes.reject(&:set?).map(&:parent).map(&:text)
p labels
#=> ["Cat", "Dog", "Pony", "black cat"]
组合地图可能更具可读性:
labels = browser.div(class: 'HOTELS').checkboxes.reject(&:set?).map { |box| box.parent.text }
p labels
#=> ["Cat", "Dog", "Pony", "black cat"]