简体   繁体   English

无法在Capybara中使用ID选中复选框

[英]Can't select checkbox using id in Capybara

There is the following RSpec code: 有以下RSpec代码:

describe 'with valid information' do
    it 'should create a new restaurant' do
      visit new_restaurant_path

    fill_in I18n.translate(:name),          with: "Some restaurant"
      fill_in I18n.translate(:address),       with: "Some address of the restaurant"
      fill_in I18n.translate(:average_check), with: 100

    check('place_restaurant_food_types_1')

    expect { click_button :submit }.to change(Restaurant, :count).by(1)
    end
end

But I always get error "cannot check field, no checkbox with id, name, or label 'place_restaurant_food_types_1' found". 但是我总是收到错误消息“无法检查字段,找不到具有ID,名称或标签'place_restaurant_food_types_1'的复选框”。 I tried to replace id for name, but it was still the same. 我尝试将id替换为名称,但是仍然相同。 I'm absolutely sure that there is the item with needed id! 我绝对确定有需要ID的商品! (I copied it from the page source). (我从页面源代码中复制了它)。 How can I fix it? 我该如何解决?

HTML: HTML:

<form accept-charset="UTF-8" action="/restaurants" class="new_place_restaurant" id="new_place_restaurant" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="7yQCirO7MLO6e+Nj46eCSNUjQWd67MJVDIHmUV6/5Y0=" /></div>
        <div class="row">
  <label for="place_restaurant_name">Название</label> 
  <input id="place_restaurant_name" name="place_restaurant[name]" size="30" type="text" />
</div>
<div class="row">
  <label for="place_restaurant_address">Адрес</label> 
  <input id="place_restaurant_address" name="place_restaurant[address]" size="30" type="text" />
</div>

        <div class="row">
            <label for="place_restaurant_average_check">Средний чек</label>
            <input id="place_restaurant_average_check" name="place_restaurant[average_check]" size="30" type="text" />
        </div>
            <div class="row">
                <input id="place_restaurant_food_types_1" name="place_restaurant[food_types][1]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_1">Восточная кухня</label>      
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_2" name="place_restaurant[food_types][2]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_2">Национальная кухня</label>       
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_3" name="place_restaurant[food_types][3]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_3">Японская кухня</label>       
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_4" name="place_restaurant[food_types][4]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_4">Китайская кухня</label>      
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_5" name="place_restaurant[food_types][5]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_5">Европейская кухня</label>        
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_6" name="place_restaurant[food_types][6]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_6">Кавказская кухня</label>     
            </div>
        <div id="map_canvas"></div>
<input id="latitude" name="place_restaurant[latitude]" type="hidden" value="54.352271" />
<input id="longitude" name="place_restaurant[longitude]" type="hidden" value="48.52652" />


        <div class="row">
            <input id="submit" name="commit" type="submit" value="Сохранить" />
        </div>
</form>

You could just use 你可以用

check("place_restaurant_food_types_1")

as shown in the docs : 文档所示:

The check box can be found via name, id or label text. 可以通过名称,ID或标签文本找到该复选框。

像这样尝试Xpath

find(:xpath, "//*[@id='place_restaurant_food_types_1']").click

你可以这样使用

find(:css,"input[id='place_restaurant_food_types_1']").set true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM