How to find specific lines in a table using Selenium?

Koray Tugay picture Koray Tugay · Jan 7, 2013 · Viewed 109.5k times · Source

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

Answer

Prashant Shukla picture Prashant Shukla · Jan 7, 2013

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.