8

I have the following html:

<html>
  <head>
    <body>
      <table>
       <tr>
      <td>Something Else</td>
      <td>Something</td>
    </tr>
    <tr></tr>
  </table>
</body>

The xpath expression for the first row second column is:

/html/body/table/tbody/tr/td[2]

I'd like to use selenium to get the value '2'.

In other words, what is the value for the first occurrence of text "Something". I'm able to get selenium to recognize that element but not return the value.

Edit: So far some great answers. But let me clarify that this is more of a selenium question as opposed to a xpath question. I need to figure out how to get selenium to return this value.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
KevinO
  • 970
  • 2
  • 13
  • 31

5 Answers5

7

The following expression returns the position (column index) of the first cell containing the desired text:

count(/html/body/table/tr/td[.='Something'][1]/preceding-sibling::td) + 1
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • how do you get it back in java. normally you do .findElement() which returns WebElement not integer ? – sten Mar 03 '17 at 17:57
3

You can get the column index by using selenium.getXpathCount('/html/body/table/tbody/tr/td') and then iterating over the range with selenium.getText('xpath=(/html/body/table/tbody/tr/td)[' + i + ']') until it returns "Something". Remember to count from 1 to n, not 0 to n-1 - XPath counts from 1, not from 0 like many programming languages.

Ross Patterson
  • 9,527
  • 33
  • 48
  • 'getXpathCount' has never returned me results, until I referenced the following question/answer: http://stackoverflow.com/questions/8448828/xpath-for-returning-the-column-index in which it refers to using 'selectFrame' before making the call to 'getXpathCount'. Thanks. – KevinO Dec 12 '11 at 16:07
  • 1
    Yeah, if you use frames, you need to tell Selenium when you're moving your focus from one to another. – Ross Patterson Dec 12 '11 at 16:47
  • OK, this question caused me to dig a little further, and it turns out Selenium RC's API has a function designed to do exactly this: `GetElementIndex(...locator...)` will find the immediate parent element of the specified element, and return its index as a child of that parent. But it returns 0 to n-1, so be sure to add 1 before using that value in an XPath. /me loves learning new things while answering questions! – Ross Patterson Dec 12 '11 at 17:55
0

Use:

/html/body/table/tr/td[text() = 'Something']

For XML:

<html>
    <body>
      <table>
       <tr>
      <td>Something Else</td>
      <td>Something</td>
    </tr>
    <tr></tr>
  </table>
</body>
</html>

it selects 2nd td element.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
0

I think what you are trying to do is dynamically get the value of the index. If I am understanding your question correctly. If that is the case why not simply store the object in a list and get the text of whatever element you want. This allows for a much more scalable parsing of the data and makes your test less brittle.

WebElement thisElement = driver.findElement(By.xPath("//table/tr/"));
List<WebElement> columns = thisElement.findElement(By.tagName("td"));

foreach(WebElement col in columns)
{
  col.Text();
}
CBRRacer
  • 4,649
  • 1
  • 23
  • 27
-2

Hope below code will help for you

public static int getColumnIndex(WebDriver driver,String columnName)
{

int columns=driver.findElements(By.xpath("//body/table/tr/td")).size();

int indexOfQuanity = 0;

for(int i=1;i<=columns;i++)
{           

if(driver.findElement(By.xpath("//body/table/tr/td["+i+"]/b")).getText().equalsIgnoreCase(columnName))
{

indexOfQuanity=i;

break;

}

}

return indexOfQuanity;

}
αғsнιη
  • 2,627
  • 2
  • 25
  • 38