Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium, xpath to match a table row containing multiple elements

I'm trying to find a Selenium/PHP XPath for matching a table row that contains multiple elements (text, and form elements).

Example:

<table class="foo">
  <tr>
    <td>Car</td><td>123</td><td><input type="submit" name="s1" value="go"></td>
  </tr>
</table>

This works for a single text element:

$this->isElementPresent( "//table/tbody/tr/td[contains(text(), 'Car')]" );

while this does NOT (omitting the /td locator):

$this->isElementPresent( "//table/tbody/tr[contains(text(), 'Car')]" );

and thus, this obviously won't work either for multiple elements:

$this->isElementPresent( "//table/tbody/tr[contains(text(), 'Car')][contains(text(), '123')]" );

Another way to do this would be with getTable( "xpath=//table[@class='foo'].x.y") for each and every row x, column y. Cumbersome, but it worked... mostly. It does NOT return the <input> tag! It will return an empty string for that cell :(

Any ideas?

like image 608
foob.ar Avatar asked May 14 '26 13:05

foob.ar


1 Answers

This XPath expression:

/html/body/table[descendant::td[contains(.,'Car')]]

Note: If you know your schema, don't use a starting // operator. Use string value instead of text node (this way you get the concatenation of all descendant text nodes).