ddoherty
ddoherty

Reputation: 375

Capybara: how to fill in an input form with no id, name, or label text?

I am trying to fill in the date field on a web page with the following using Capybara:

<div class="activity--history-custom-dates-filter-container">
  <label class="activity--header-element-label">From </label>
  <input class="activity--history-custom-date-field
       fid-datepicker--input activity-- history-custom-date-from-field hasDatepicker"
       placeholder="mm/dd/yyyy" id="dp1416484110853">
  <button class="activity--calendar-button"></button>
</div>

I can get to the parent div with the following:

within :xpath, "//label[text()='From ']/.." do
  fill_in ????, with: period.first.american
end

I would like to fill in the input field with the date as shown, but I can't seem to find the right 'locator' (shown as ????) as the first argument to fill_in. The id is generated programmaticly so it changes on each visit to the page, so that's not acceptable.

The Capybara docs describe fill_in as follows:

"Locate a text field or text area and fill it in with the given text The field can be found via its name, id or label text."

Apparently, 'name' here means any text associated with the node, not the name of the element 'input' here.

I'm at a loss as to how to proceed. Any ideas?

Upvotes: 1

Views: 2278

Answers (2)

Jeff
Jeff

Reputation: 1222

You should be able to do this in a prettier way with css as well, like so:

within '.activity--history-custom-dates-filter-container' do
  find('input').set period.first.american
end

Upvotes: 3

Oleksii Matiiasevych
Oleksii Matiiasevych

Reputation: 261

You are limited to use within block? If not maybe do like this:

find(:xpath, "//label[text()='From ']/../input").set period.first.american

Upvotes: 4

Related Questions