Selenium, xpath to match a table row containing multiple elements

19,098

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

//tr/td[contains(text(), 'Car')]/text()  | //tr/td/input[@value="s1"]/@name
Share:
19,098
foob.ar
Author by

foob.ar

Updated on June 04, 2022

Comments

  • foob.ar
    foob.ar about 2 years

    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?