views:

37

answers:

3

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 tag !? It will return an empty string for that cell :(

Any ideas ? Thanks in advance.

+2  A: 

This XPath expression:

/html/body/table[descendat::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).

Alejandro
While this may work, I prefer not to use a full schema, it's less flexible (e.g. if another developer adds something on the page before my table, the xpath breaks). Also it doesn't solve my original problem of matching one row containing multiple specified elements.
foob.ar
@foob.ar: That's why my sentece begins with `If you know your schema`... Your problem is for ussing `text()` and your question is `Any ideas ?`. You could modify this if you know how to make generalizations. Do you want the `td` containers to be differents or it may be the same?
Alejandro
A: 

you might want to use

//td[contains,'Car'] and td[contains,'123']/ancestor::tr

that will select the tr that contains td which matches the two contains arguments

Try to use View Xpath Plugin in firefox, very useful plugin.

Learn more about Axes in Xpath: http://www.w3schools.com/xpath/xpath_axes.asp

Javabeginner
Thanks, this seems to work as needed ! Yes this firefox plugin is nice, although it doesn't spit out flexible xpaths - as said in my other comment to Alejandro, if something is added to the page before my table, the xpath will break.
foob.ar
@foob.ar: This has syntax errors...
Alejandro
The syntax is not been tested, sorry about the error. I doubt that will break the xpath as the xapth select an element 1- the content of td a and b, nothing to do with the location of the table and what follow or go in front of it. Hope I get this right..
Javabeginner