0
我正在尝试编写一个自定义方法,允许使用我们的自定义select2 country_search
元素。这些元素都有点靠不住,但HTML的代码片段看起来是这样的:使用Capybara Select select2选择范围
<div class="control-group select optional admins_customer_form_object_country_id">
<label class="select optional control-label" for="admins_customer_form_object_country_id">Country</label>
<div class="controls">
<select id="admins_customer_form_object_country_id" class="select optional select2-hidden-accessible" name="admins_customer_form_object[country_id]" tabindex="-1" aria-hidden="true">
<option value=""></option>
<option value="true">Yes</option>
<option value="false">No</option>
</select>
<span class="select2 select2-container select2-container--classic select2-container--below select2-container--focus" dir="ltr" style="width: 220px;">
<span class="selection">
<span class="select2-selection select2-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-admins_customer_form_object_country_id-container">
<span class="select2-selection__rendered" id="select2-admins_customer_form_object_country_id-container">
<span class="select2-selection__placeholder">Select Country</span>
</span>
<span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b>
</span>
</span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span>
</span>
</div>
</div>
我写了一个帮手应该选择正确的span
并在其上进行点击,但我坚持选择我想要的跨度。我试图选择具有类select2 select2-container select2-container--classic select2-container--below select2-container--focus
的外部可见范围,但我一直收到无效的XPath错误。
这是我到目前为止有:
def select_country(label:, value:)
# Select our label first
label_element = first("label", text: label)
# Now navigate through the entire tree, and click the correct SPAN element.
within(label_element) do
select2_container = find(:xpath, "..") # Up one level to the parent div
select2_container = select2_container.find("div.controls") # Down one level into the div.container
select2_container = select2_container.find(:xpath, "./*[1]") # select the span element surrounding all.
end
end
随着最后一行我可以选择从树中select
元素,但我不能让span
兄弟姐妹不管我试试。
谢谢你,但我已经缩小了这个问题。这不是XPATH无效,问题是SPAN元素不在那里。由于某些原因,当Capybara加载页面时,即使使用Poltergeist,JavaScript也不会被执行。 –
检查您的浏览器控制台中是否有任何错误。通常在开发模式下,JS文件被单独加载,因此一个错误不会阻止其他人执行,但是在测试模式下,它们会被连接成一个JS文件,这意味着只有一个JS文件中的错误可以阻止在其他人的初始化代码中执行。 –
这是JS未加载,因为测试是在不支持JS的标准Rack Runner下运行的。我已经重写了整个测试设置,现在使用Poltergheist,并且一切正常。添加你的产品线的速度要快得多。 –