Ruby-on-rails 使用 Capybara + Rails3 找不到要单击的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11335303/
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
Can't find element to click on using Capybara + Rails3
提问by Deleteman
Background: I'm using Capybara with Rspec to test a Rails 3 app. Driver used: Selenium
背景:我使用 Capybara 和 Rspec 来测试 Rails 3 应用程序。 使用的驱动程序:硒
Problem: I can't find the "Sign in" button in order to click on from within my test.
问题:我找不到“登录”按钮以便在我的测试中单击。
HTML code:
HTML代码:
<form accept-charset="UTF-8" action="/" class="filter_form" id="login" method="post">
<fieldset>
<div class="modal-body">
<div class="clearfix login-fields">
<label for="user_email">Email</label>
<div class="input login-inputs">
<input class="input-text" id="user_email" name="user[email]" placeholder="email" size="30" type="email" value="">
</div>
</div>
<div class="clearfix login-fields">
<label for="user_password">Password</label>
<div class="input login-inputs">
<input class="input-text" id="user_password" name="user[password]" placeholder="password" size="30" type="password">
</div>
</div>
</div>
<div class="modal-footer">
<input class="btn btn-primary login_btn" id="btn_login" name="commit" type="submit" value="Sign in">
<a href="/lms/forgot_password" class="btn">Forgot password...</a>
<a href="#" class="btn close cancel" data-dismiss="modal">Cancel</a>
</div>
</fieldset>
</form>
Failing test
未通过测试
it "should login correctly if the right credentials are given", :js => true do
Capybara.default_wait_time = 5
Capybara.reset_sessions!
visit '/'
click_link('login_link') #this will bring a modal window with the code posted above using js
within("#login") do
fill_in 'user_email', :with => "[email protected]"
fill_in 'user_password', :with => "mypwd"
end
response.should have_selector('input#btn_login') #This passes
click_on("input#btn_login") #Here it fails, saying it can't find an element with that selector
response.should have_selector(:xpath, '//div[@class="alert-message block-message info"]')
end
My test file is inside spec/requests.
我的测试文件在里面spec/requests。
Any ideas? Thanks!
有任何想法吗?谢谢!
回答by suresh.g
Please try this:
请试试这个:
page.find("#btn_login").click
回答by jnicklas
This: https://stackoverflow.com/a/11348065/1504796
这个:https: //stackoverflow.com/a/11348065/1504796
Is the right answer.
是正确答案。
click_ondoes not take a CSS selector, but instead the text or id of a link. You want click_on("btn_login"). No hash sign or anything.
click_on不采用 CSS 选择器,而是采用链接的文本或 ID。你要click_on("btn_login")。没有哈希符号或任何东西。
回答by prasad.surase
try
尝试
find('#id',:visible => true).click
回答by jasnow
Try adding "gem 'launchy'" to your Gemfile and put "save_and_open_page" line before failed line in step file.
尝试将“gem 'launchy'”添加到您的 Gemfile 并在步骤文件中的失败行之前放置“save_and_open_page”行。
REFERENCE: http://techiferous.com/2010/04/using-capybara-in-rails-3/
参考:http: //techiferous.com/2010/04/using-capybara-in-rails-3/
回答by sirvine
It looks like you're trying to click on a submit button inside some modal css. You'll need to invoke whatever displays that modal element first.
看起来您正在尝试单击某个模态 css 中的提交按钮。您需要首先调用显示该模态元素的任何内容。
回答by ebeland
I don't think click_on will take a locator like that--I think it may want just an id, name or value. As an experiment, try replacing click_on("input#btn_login") with:
我认为 click_on 不会采用这样的定位器——我认为它可能只需要一个 id、名称或值。作为实验,尝试将 click_on("input#btn_login") 替换为:
page.find('#btn_login').click
page.find('#btn_login').click
回答by user2393426
- click_button("Sign in")
- find(:xpath, "//*[@id='btn_login']").click (or .trigger('click'))
- click_button("登录")
- find(:xpath, "///*[@id='btn_login']").click (或 .trigger('click'))
If else fails, go through the console and see if you can check the presence of the button: document.getElementById("btn_login")
如果失败,请通过控制台查看是否可以检查按钮是否存在:document.getElementById("btn_login")
回答by Stefan Lance
回答by Gabriel C
I use capybara with chrome
我用镀铬的水豚
Capybara.register_driver :chrome do |app| Capybara::Selenium::Driver.new(app,
:browser => :chrome)
end
Capybara.javascript_driver = :chrome
and install chrome driver:
并安装 Chrome 驱动程序:
brew install chromedriver
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/
回答by Jorge
Try adding a save_and_open_page before the line in question. This should allow you to see the page and any errors that may prevent the clicking action.
尝试在相关行之前添加 save_and_open_page。这应该允许您查看页面以及可能阻止单击操作的任何错误。

