How to find specific lines in a table using Selenium?
Asked Answered
P

6

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'>

Pinch answered 7/1, 2013 at 7:54 Comment(0)
D
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.

Discount answered 7/1, 2013 at 8:27 Comment(3)
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 <TR>s in the entire document, not in the specific table.Squaw
My Mistake... the By.className wasn't supposed to be empty. copy paste error. Thanks @RossPattersonDiscount
@KorayTugay if the solution did not work for please let me know with more info. I will be glad to help.Discount
S
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'>"

Squaw answered 7/1, 2013 at 12:5 Comment(0)
D
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

Dobbin answered 7/1, 2013 at 8:34 Comment(2)
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().Squaw
It needs to be tableRow.findElement(By.xpath("td[2]")), right?Blamed
H
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

Hambley answered 25/10, 2016 at 13:24 Comment(0)
P
0
(.//*[table-locator])[n]

where n represents the specific line.

Pyxie answered 8/6, 2015 at 19:1 Comment(0)
L
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();
Limitless answered 18/5, 2018 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.