Search code examples
htmlruby-on-railsrubyxpathcapybara

Get attribute value in Capybara


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?


Solution

  • 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>