1

I have an HTML partial that looks like this:

<input id="order_date" name="order_date" class="date-applied"
 onchange="restoreDate=false;" type="text" value="01/05/2016" 
 size="12" maxlength="10"/>

I need to retrieve the value of attribute value using Capybara. I have tried using this code, without success:

find(:xpath,"//table[2']/tbody/tr[7]/td[@name='order_date]")['value']

How do I make this work?

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
Jennifer
  • 45
  • 1
  • 9
  • Looks like a duplicate of this question - http://stackoverflow.com/questions/13906994/capybara-is-it-possible-to-get-an-attribute-value-from-a-css-string – Jon May 23 '16 at 08:03
  • Have tried the above solution as well. it retrieves nil when tried to get with the attribute [:'value'] or [:value]. not sure what i was missing. – Jennifer May 23 '16 at 08:29
  • @Jon I don't believe that this is a duplicate of [http://stackoverflow.com/questions/13906994/capybara-is-it-possible-to-get-an-attribute-value-from-a-css-string](http://stackoverflow.com/questions/13906994/capybara-is-it-possible-to-get-an-attribute-value-from-a-css-string), as asks how to query. With this question, however, it appears that the OP knows how to do this, but simply can't get it to work. They are certainly related, but I don't believe that they're the same. – Michael Gaskill May 23 '16 at 09:04

1 Answers1

2

You might check that your xpath returns a value. The xpath that you used as an example has multiple syntax errors. Try this, instead:

find(:xpath, "//table[2]/tbody/tr[7]/td/input[@name="order_date"]")['value']

I created a structurally correct test document based on what it appears you're searching for. The xpath above found the input element in this document:

<document>
    <table></table>
    <table>
        <tbody>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr></tr>
            <tr>
                <td>
                    <input id="order_date" name="order_date" class="date-applied"
                     onchange="restoreDate=false;" type="text" value="01/05/2016" 
                     size="12" maxlength="10"/>
                </td>
            </tr>
        </tbody>
    </table>
</document>
Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
  • Thanks Michael for the great explanation. It worked. i missed the input[] tag in the code as you mentioned.. the quote marks , i mistyped it. – Jennifer May 23 '16 at 09:46