我正在测试这个页面,我必须填写一个表单,并且只有当所有字段都填满并且有效时,按钮才能启用,但是,当试图找到要点击的按钮时,我总是得到capybara的消息无法找到按钮或元素,但我试图找到它。该按钮没有名称或ID。
我尝试了我在网上找到的所有内容,但似乎没有任何效果。这是我在浏览器上检查时按钮的代码:
<div class="button-set terms ng-scope">
<button class="btn btn--highlight ng-binding" ng-disabled="shippingAddressForm.$invalid || (!shop.useSameAddress && billingAddressForm.$invalid) || view.loading" ng-class="{ disabled: shippingAddressForm.$invalid || (!shop.useSameAddress && billingAddressForm.$invalid),
loading: view.loading }" ng-click="shop.updateBothAddresses(view.shippingAddressModel, view.billingAddressModel)" style="">
" Continuar "
<span class="spinner"></span>
</button>
但我总是得到错误,无论我试图找到什么,都找不到。
答案 0 :(得分:0)
您的初始尝试click_button(' Continuar ')
不起作用,因为Capybara通常使用XPath函数normalize-space
- https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/normalize-space对文本内容进行规范化 - 在比较时,前导空格和尾随空格将被删除。尝试
click_button('Continuar')
您的第二次尝试find(:xpath, '//button[@class="btn.btn--highlight.ng-binding"]')
不起作用,因为您正在混合使用XPath和CSS语法。要在不使用XPath的情况下查找按钮,您可以完成
# Note the leading . on the XPath selector - see https://github.com/teamcapybara/capybara#beware-the-xpath--trap
find(:xpath, './/button[@class="btn btn--highlight ng-binding"]').click # in XPath it's just the attribute value - there is no . shortcut for classes
或使用CSS
find(:css, 'button.btn.btn--highlight.ng-binding').click # the first argument :css is optional if Capybara.default_selector has the default value of :css
或使用Capybara按钮查找器
find_button(class: %w(btn btn--highlight ng-binding)).click # or
click_button(class: %w(btn btn--highlight ng-binding))