Selenium is an open-source browser automation tool. So, while automating any functionality first we have to locate the element. Capybara provides two methods to locate elements:
Let suppose we have an input textbox that takes email as as argument. So, we can use both the methods to enter text in the input field:
<input id="email" class="forgotPswd" type="text" value="" name="email"/>
@session.fill_in '#email', with: email
The first method works fine but the second method is unable to find element and giving error that: “Unable to locate “#email”
The reason for this uneven behavior is that locator for find() and fill_in() method is different. find() method assumes that the first parameter is the default selector(xpath or css) if the first parameter is not a symbol. The first parameter in fill_in method is id, name or label text. So, when using find method then do not pass xpath or css as an argument.
@session.fill_in 'email', with: email
In the above method, now you can see that me passed only the id of element without any symbol then capybara has easily located the element.