james
james

Reputation: 4049

Capybara fill_in with xpath

I have multiple input fields with one id specification, but they have different attributes. I can find them just fine, but am having trouble figuring out how to use the fill_in with the find appropriately. Currently I have:

find('//input[@id="specification"][@data-number="1"]').fill_in with: "This first sleeping bag should be 0 degrees F"

Error-- Capybara::Webkit::InvalidResponseError:SYNTAX_ERR: DOM Exception 12

Thanks!

Upvotes: 1

Views: 4076

Answers (2)

Justin Ko
Justin Ko

Reputation: 46836

The first is that fill_in will look for a text field within the element returned by find. Basically you are asking Capybara to find something like:

<input id="specification" data-number="1">
  <input type="text">
</input>

This is unlikely to exist.

Given that you have already found the text field using find, you want to use the set method to input it:

find('//input[@id="specification"][@data-number="1"]').set("This first sleeping bag should be 0 degrees F")

Assuming you have not changed the default_selector to :xpath, the above will still fail due to the expression not being a valid CSS-selector. You need to also tell Capybara that an :xpath locator is being used:

find(:xpath, '//input[@id="specification"][@data-number="1"]').set("This first sleeping bag should be 0 degrees F")

Upvotes: 4

Tim Moore
Tim Moore

Reputation: 9482

It's not valid HTML to have multiple elements with the same id. You should start by changing that.

Upvotes: 3

Related Questions