开发者

Selenium, xpath to match a table row containing multiple elements

开发者 https://www.devze.com 2023-01-22 11:42 出处:网络
I\'m trying to find a Selenium/PHP XPath for matching a table row that contains multiple elements (text, and form 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?


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).


Several paths can be combined with | separator. Tweak this:

//tr/td[contains(text(), 'Car')]/text()  | //tr/td/input[@value="s1"]/@name


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


Thanks to knb for some syntax hints. This is slightly off-topic, but relevant to the search that led me here...

I had a table with [ name | value ] cells. I needed to get value from the row with 'name' preceding it.

(fake example, but every link I was looking for had the same text and no IDs - the point is that the context information was in a neighboring cell)

<table id="options"><tbody>
<tr>
 <td>other</td>
 <td><a href="#clicky">edit</a></td>
</tr>
<tr>
 <td>this label</td>
 <td><a href="#clicky">edit</a></td> <!-- I want this button -->
</tr>
<tr>
 <td>other</td>
 <td><a href="#clicky">edit</a></td>
</tr>
</tbody></table>

I could retrieve the button I wanted like this, using nested [[]] conditions:

//table[@id='options']/tbody/tr[td[contains(text(), 'this label')]]/td[2]/a

"get the "a" that is in a row that contains another cell with the text I'm looking for"

I think this sort of task might be a common case, so I'm posting it here FYI


In my problem, I had a list of products where it was identified by a unique SKU/catalog combination. If I wanted to add that product to a cart, I chose it by SKU and catalog.

Using foob.ar's example:

//table[@class='foo']/tr[td[contains(text(), 'Car')] and td[contains(., '123')]]

You can combine it with dman's solution for choosing a specific element/column within that row

//table[@class='foo']/tr[td[contains(text(), 'Car')] and td[contains(., '123')]]//input[@name='s1']

Edit:

The solution above works if I was only looking for those two values in any of the columns. If you want to find a value relative to a specific column, I had to modify it a bit

//table[@class='foo']/tr[td[position()=1 and contains(text(), 'Car')] and td[position()=2 and contains(text(), '123')]]//input[@name='s1']
0

精彩评论

暂无评论...
验证码 换一张
取 消