Pregunta

I am using Behat and am trying to select some form elements.

My HTML is as follows - Basically I have two required fields, one an input and one a textarea, and a checkbox.

<table class="table">
    <thead>
        <tr>
            <th>Delete</th>
            <th>Question</th>
            <th>Reference</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" name="delete" /></td>
            <td><textarea name="question" required="required"></textarea></td>
            <td><input type="text" required="required" name="reference" /></td>
        </tr>
    </tbody>
</table>

My xpath selector that gets the form elements is:

 //table/tbody/tr/td/input

And this works fine for inputs obviously - but when I added the textarea and tried to check it, it can't find the textarea, because duh, it's not an input.

I tried this: //table/tbody/tr/td/(input or textarea) and several variations of using "or", |, etc. I can't figure out how to specify multiple types of elements to try to match.

Thanks In Advance! :)

¿Fue útil?

Solución

PHP's XPath Processor only supports XPath 1.0, which does not allow alternations in path steps. A valid XPath 2.0 expression would have been

//table/tbody/tr/td/(input, textarea)

XPath 1.0 requires you to either provide full paths like this:

//table/tbody/tr/td/input | //table/tbody/tr/td/textarea

or use a predicate with name-test while using the wildcard node test:

//table/tbody/tr/td/*[local-name() = 'input' or local-name() = 'textarea']

The latter version will be probably preferable regarding performance as the XML file will only be scanned once.

Otros consejos

Untested in Behat/PHP, but this is how it would look if following the XPath syntax.

//table/tbody/tr/td/input | //table/tbody/tr/td/textarea
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top