Michael L.
Michael L.

Reputation: 272

How to query an HTML element in Selenium via Xpath?

After reading a lot of code examples I tried to find the <tr> elements where the content of the descendant <span> element matches the parameter columnValue and I only want to search in the column with the index given in columnId. I tried something like this in with Selenium in Java, but I always get an InvalidSelectorException: Xpath expression cannot be evaluated. I tested both conditions individually in this Xpath expression and they work without any problem. But when I combine them with the and operator in one single XPath Expression, the Exception is thrown.

This is my Java code:

public WebElement getRow(final String columnId, final String columnValue) {
      final List<WebElement> eleList =
             getElement().findElements(
                   By.xpath("tbody/tr[td/span/text()='" + columnValue + "' and ./td/position()=" + (columnId + 1)"]"));
      return null;
}

This is the corresponding HTML Code:

<tbody>
  <tr class="even">
    <td>
      <span>Regionalmarkt Nord</span>
    </td>
    <td>
      <span>777</span>
    </td>
    <td>
      <span id="links">        
        <a name="delete_button" href="#" > <img title="Delete" src="/path/..."/></a>
        <a id="edit_button" href="#"> <img title="Edit" src="/path/..."/></a>  
       </span>
    </td>
  </tr>
  <tr>
  ...
  </tr>
</tbody>

Upvotes: 2

Views: 168

Answers (2)

drkthng
drkthng

Reputation: 6939

try this xpath expression:

"//td[" + (columnId + 1) + "]/span[text()='"+ columnValue +"']/ancestor::tr[1]"

Upvotes: 2

Pawan B
Pawan B

Reputation: 4623

You can use firebug to get xPath for the element .

Refer this article :

http://www.wikihow.com/Find-XPath-Using-Firebug

Check this link for similar question :

How to get the absolute XPath for an element within Firebug?

Upvotes: 0

Related Questions