2016-09-28 10 views
0

我的HTML/ERB看起来像这样水豚并不想选择一个输入节点

<fieldset class="row notifications"> 
    <legend> 
     <hr class="dash blue inline"> 
     <%= t :my_notifications %> 
    </legend> 

    <label> 
     <%= f.check_box(:subscribed_to_news) %> 
     <span></span> 
     <span class="checkbox-text"><%= t :accepts_to_receive_news %></span> 
     <br> 
    </label> 
</fieldset> 

当调试我的黄瓜测试与水豚,我找到了该通知复选框f.check_box(:subscribed_to_news)这个烂摊子

page.find('.notifications label')['innerHTML'] 
# => "\n\t\t<input name=\"user[subscribed_to_news]\" type=\"hidden\" value=\"0\"><input type=\"checkbox\" value=\"1\" checked=\"checked\" name=\"user[subscribed_to_news]\" id=\"user_subscribed_to_news\">\n\t\t<span></span>\n\t\t<span class=\"checkbox-text\">blahblahblah</span>\n\t\t<br>\n\t" 

但由于某些原因,我无法找到嵌套的投入也不是由ID

page.find('.notifications label input') 
# => Capybara::ElementNotFound Exception: Unable to find css ".notifications label input" 
page.find('.notifications label #user_subscribed_to_news') # => Capybara::ElementNotFound Exception: Unable to find css ".notifications label #user_subscribed_to_news" 

选择标签找到他们虽然

page.find('.notifications label') 
# => #<Capybara::Node::Element tag="label" path="//HTML[1]/BODY[1]/DIV[1]/MAIN[1]/SECTION[1]/FORM[1]/FIELDSET[3]/LABEL[1]"> 

我做错了什么?我只想检查该死的复选框:'(

回答

0

这似乎是复选框是通过正常的CSS/XPath的可达...

我把他用一些JavaScript

page.execute_script(%Q{document.querySelector('#{area} input##{selector}').click()}) 
0

最有可能的原因是该复选框实际上被CSS隐藏,然后用图像替换,以在不同浏览器中启用相同的复选框样式如果您使用最新的Capybara可以把它单击它的标签时,该复选框是通过调用

page.check('user_subscribed_to_news', allow_label_click: true) # you can also set Capybara.automatic_label_click = true to default to this behavior 

,或者隐藏使用的是旧水豚,你会需要自己点击标签

page.find(:label, "blahblahblah").click #match on labels text 

page.find(:label, for: 'user_subscribed_to_news').click #match on labels for attribute if set