11

Here is an example code:

<div id="productOrderContainer">
  <table class="table gradient myPage">

So this table being in productOrderContainer has several columns and depending on several things will have several rows which all have several columns. An example is:

What I want to do is to for example get the first row of this table. ( rows have id's such as: <td rowspan="1"> ) And then again for example in this rowspan look for a specific value in a specific <div>

So in psudo-code what I want to say is:

Get me the table, get me the n'th row, get me the value in <div id='something'>

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

6 Answers6

40

you can try following

int index = 0;
WebElement baseTable = driver.findElement(By.className("table gradient myPage"));
List<WebElement> tableRows = baseTable.findElements(By.tagName("tr"));
tableRows.get(index).getText();

You can also iterate over tablerows to perform any function you want.

Prashant Shukla
  • 1,391
  • 9
  • 18
  • That doesn't look right at all. `WebDriver.findElement(By.className(""))` will locate *every* element in the document, not the table the OP is looking for. So the next line will collect all the ``s in the entire document, not in the specific table. – Ross Patterson Jan 07 '13 at 11:59
  • My Mistake... the By.className wasn't supposed to be empty. copy paste error. Thanks @RossPatterson – Prashant Shukla Jan 07 '13 at 14:16
  • @KorayTugay if the solution did not work for please let me know with more info. I will be glad to help. – Prashant Shukla Jan 09 '13 at 11:53
5

You want:

int rowNumber=...;
string value = driver.findElement(By.xpath("//div[@id='productOrderContainer']/table/tbody/tr[" + rowNumber +"]/div[id='something']")).getText();

In other words, locate <DIV> with the id "something" contained within the rowNumberth <TR> of the <TABLE> contained within the <DIV> with the id "productOrderContainer", and then get its text value (which is what I believe you mean by "get me the value in <div id='something'>"

Ross Patterson
  • 9,527
  • 33
  • 48
2

Well previously, I used the approach that you can find inside the WebElement:

WebElement baseTable = driver.findElement(By.tagName("table"));
WebElement tableRow = baseTable.findElement(By.xpath("//tr[2]")); //should be the third row
webElement cellIneed = tableRow.findElement(By.xpath("//td[2]"));
String valueIneed = cellIneed.getText();

Please note that I find inside the found WebElement instance.

The above is Java code, assuming that driver variable is healthy instance of WebDriver

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • 1
    That won't work as written because `WebElement.findElement(By.xpath(...))` interprets the XPath locator in the document context, not in the element context like every other `By.whatever()`. – Ross Patterson Jan 07 '13 at 11:57
  • It needs to be `tableRow.findElement(By.xpath("td[2]"))`, right? – Kai Hartmann Oct 01 '21 at 14:27
2

if you want to access table cell

WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[1]")); 

If you want to access nested table cell -

WebElement thirdCell = driver.findElement(By.Xpath("//table/tbody/tr[2]/td[2]"+//table/tbody/tr[1]/td[2]));

For more details visit this Tutorial

anuja jain
  • 1,367
  • 13
  • 19
0
(.//*[table-locator])[n]

where n represents the specific line.

Dan Getz
  • 8,774
  • 6
  • 30
  • 64
Anoop Philip
  • 971
  • 2
  • 11
  • 19
0

The following code allows you to specify the row/column number and get the resulting cell value:

WebDriver driver = new ChromeDriver();
WebElement base = driver.findElement(By.className("Table"));
tableRows = base.findElements(By.tagName("tr"));
List<WebElement> tableCols = tableRows.get([ROW_NUM]).findElements(By.tagName("td"));
String cellValue = tableCols.get([COL_NUM]).getText();