When we want to select a value from a table but the element doesn’t have id or other identification, we can use the other cells of the table to identify the value.

For example:


<table id="business_marketData_items" summary="Current market values for major international indexes">
<td><a href="http://www.bbc.co.uk/news/business/market_data/stockmarket/2/">Dow Jones</a></td>
<td><a href="http://www.bbc.co.uk/news/business/market_data/stockmarket/12122/"> Nasdaq</a></td>

There is a table with stock market data and we want to get the value of the percentage of change (the last column). The percentage doesn’t have any identification, but we know the name of the stock market. For example we choose Dow Jones and the desired value is 0.72%.

How to get the value?

We can use Xpath to find the element.

Here is the solution:

 “By.xpath(“.//a[contains(text(),’Dow Jones’)]/ancestor::td/following-sibling::td[last()]”)).getText();”

To find the “Dow Jones” text we use the “a[contains(text(),’ ’)]” command.

Because the <a> link element is inside of a <td> element firstly we use the “ancestor::td” command. The ancestors of the context node consist of the parent of context node and the parent’s parent and so on, but we use the “td“ letters instead of “*” to select only the parent <td>.

After that, we want to select the last <td> which contains the desired value.

With the “following-sibling::td” command we can select the following <td>-s. The following-sibling axis contains all the following siblings of the context node.

Because we want to select the last <td> we can use the “td[last()]”  command. A number in the brackets (for example: td[2] ) gives the position of the element in the selected set. The function last() selects the last element in the selection.

After all of that we can use the ”getText()” function to get text from the selected cell.

Here is the full code:

public void testFindElements()throws Exception{
String stockmarket= "Dow Jones";
WebElement businessTable= driver.findElement(By.id("business_marketData_items"));

for(int i =1; i<6;i++){
WebElement tableRows= businessTable.findElement(By.xpath(".//tbody/tr["+i+"]"));
String txt = tableRows.getText();

String percentage = tableRows.findElement(By.xpath(".//a[contains(text(),'"+stockmarket+"')]/ancestor::td/following-sibling::td[last()]")).getText();



The output is: 0.72%

Posted By István Lackó

    2 Responses to “How to select an element from table if there is no ID”

  1. Vaibhav says:

    Plus One…Good Explanation You mentioned last() to select the Can u please list other functions that are commonly used

  2. We use a couple of other functions to navigate until the required element.
    For instance string text() is frequently used to locate the element based on its content:

    //a[text () = 'Dow Jones']

    The expression gives us all the <a> tags containing the “Dow Jones” text.
    It is possible to filter the tags based on their content of attributes with function boolean contains(string, string) just like that:

     //a[contains(@href, ‘market_data’)]

    That results all <a> tags with href attribute which contains “market_data” string.
    Also very useful tool is the negation of other expressions with following function boolean not (object).

    The usage of the correct expression or function always depends on the current situation. Feel free to check the xpath documentation or one of the cheat sheets available on the internet.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>