Finders should be very powerful to locate elements on the webpage. Capybara provides a wide range of finders to locate the elements:
- find
- find_field
- find_link
- find_button
- find_by_id
- find_labeled
Let's suppose we have the following HTML
<div class="fields">
<label for="user_session_password">Password</label>
<input id="user_session_password" type="password" size="30" onkeydown="preventSpaceOnkeyDown(event);" name="user_session[password]"/>
<span id="pass_session_password_error" style="display:none;float: right; padding-right: 17px;">Password should have minimum 6 characters</span>
<span id="pass_session_password_blank_error" style="display:none;float: right; padding-right: 17px;">Please enter password</span>
</div>
<div class="fields">
<label>Remember Me</label>
<input type="hidden" value="0" name="user_session[remember_me]"/>
<input id="user_session_remember_me" class="check_box" type="checkbox" value="1" style="float:left;width:15px;" name="user_session[remember_me]"/>
<input id="user_session_submit" class="submit signin" type="submit" value="" name="commit"/>
</div>
<div class="fields" style="text-align:center;margin-top:13px;">
<a class="signLinkColor forgotPswdSignIn" href="javascript:showOverlay("forgotPasswordOverlay");">Forgot Password?</a>
</div>
find: This method takes CSS selector or Xpath expression as an argument and returns Capybara::Element on which the desired action is performed.
Locate the Password field and perform the click action.
page.find('#user_session_password').click
find_field: This capybara method locate the element with the label name or name/ id attribute
Locate the Password field with the label name
page.find_field('Password')
find_link: This capybara method locate the image link or the anchor tag through id, text or image alt attribute
Locate the Forgot Password link with the text
page.find_link('Forgot Password?')
find_button: This capybara method locates the button through name, id or value attribute
Locate SignIn button through id attribute
page.find_button('user_session_submit')
find_by_id: This capybara method locates the element on webpage through id attribute
Locate password error message
page.find_by_id('pass_session_password_error')
find_labeled: This capybara method is same as find_field method and it finds element with the label name
Locate "Remember Me" text
page.find_labeled('Remember Me')
0 Comment(s)