ruby Capybara - 按类名单击元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43396872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capybara - Click element by class name
提问by King
For what seems to be a simple question I've been on this for a stupidly long time and can't seem to find anything on Google. I have this button I need to click which has no id but a class is included
对于一个看似简单的问题,我已经愚蠢地研究了很长时间,似乎在谷歌上找不到任何东西。我有这个按钮我需要点击它没有 id 但包含一个类
<button class="filter-case-studies" onclick="initBootpag(filterForContentType('CASE STUDIES', searchHits))" type="button">
<b>CASE STUDIES</b>
(2)
</button>
I've tried using click_onwhich I now know is only for links and buttons so of course won't work. This is what I have so far:
我试过使用click_on,我现在知道它只用于链接和按钮,所以当然行不通。这是我到目前为止:
When(/^I filter the results to only see case studies$/) do
click_on('filter-case-studies')
end
I've also tried page.find('filter-case-studies').click, this too doesn't work.
我也试过page.find('filter-case-studies').click,这也行不通。
page.find(:class, 'filter-case-studies').clickdefualts to :css so this also failed for me.
page.find(:class, 'filter-case-studies').click默认为 :css 所以这对我来说也失败了。
Is there no way to click an element by the class name in Capybara?
有没有办法在 Capybara 中按类名单击元素?
Thanks in advance for the help.
在此先感谢您的帮助。
回答by Thomas Walpole
The standard way of doing this in Capybara is
在 Capybara 中这样做的标准方法是
find('button.filter-case-studies').click
In relatively recent versions of Capybara you should also be able to do
在相对较新的 Capybara 版本中,您也应该能够做到
click_on(class: 'filter-case-studies')
回答by António Ferreira
find('.filter-case-studies').clickas recommended here https://robots.thoughtbot.com/write-reliable-asynchronous-integration-tests-with-capybara#find-the-first-matching-element
find('.filter-case-studies').click如此处推荐https://robots.thoughtbot.com/write-reliable-asynchronous-integration-tests-with-capybara#find-the-first-matching-element
回答by avjaarsveld
I had a button that could not be found (Unable to find visible link or button nil with classes [close-modal]) using the methods above.
我有一个无法Unable to find visible link or button nil with classes [close-modal]使用上述方法找到的按钮 ( )。
This worked for me: page.execute_script('$.find(".close-modal")[0].click()')
这对我有用: page.execute_script('$.find(".close-modal")[0].click()')
回答by tmikeschu
click_on('.filter-case-studies')
click_on('.filter-case-studies')
You need the .selector for classes, and #for ids.
您需要.类和#id的选择器。
回答by King
Thanks to Mr Schutte for the idea to use .selectors.
感谢 Schutte 先生提出使用.选择器的想法。
I had to use page.find(:class, '.filter-case-studies').clickin the end. The absolute navbar got in the way of the button so I then had to include page.execute_script "window.scrollBy(0,500)"to complete the test.
最后我不得不使用page.find(:class, '.filter-case-studies').click。绝对导航栏挡住了按钮,所以我不得不包括page.execute_script "window.scrollBy(0,500)"来完成测试。

