Whenever the pagem loads its take the table data dynamically and get the table data.
In this Example am going to take table data for the below table.
Source code :
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class testpins {
WebDriver driver = new FirefoxDriver();
@BeforeTest
public void setup() throws Exception {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://www.moneycontrol.com/");
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void Handle_Dynamic_Webtable() {
// To locate table.
WebElement mytable = driver.findElement(By.xpath("//*[@id='tblrMC']"));
// To locate rows of table.
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
// To calculate no of rows In table.
int rows_count = rows_table.size();
// Loop will execute till the last row of table.
for (int row = 0; row < rows_count; row++) {
// To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(
By.tagName("td"));
// To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row " + row + " are "
+ columns_count);
// Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number " + row
+ " and column number " + column + " Is " + celtext);
}
System.out
.println("--------------------------------------------------");
}
}
}
If you run the above code your output look like this,
Number of cells In Row 0 are 0
--------------------------------------------------
Number of cells In Row 1 are 5
Cell Value Of row number 1 and column number 0 Is Gabriel India
Cell Value Of row number 1 and column number 1 Is Nov 5th 2014
Cell Value Of row number 1 and column number 2 Is 83
Cell Value Of row number 1 and column number 3 Is 99.25
Cell Value Of row number 1 and column number 4 Is 19.00
--------------------------------------------------
Number of cells In Row 2 are 5
Cell Value Of row number 2 and column number 0 Is SKF India
Cell Value Of row number 2 and column number 1 Is Oct 8th 2014
Cell Value Of row number 2 and column number 2 Is 1116
Cell Value Of row number 2 and column number 3 Is 1472.50
Cell Value Of row number 2 and column number 4 Is 31.90
--------------------------------------------------
Number of cells In Row 3 are 5
Cell Value Of row number 3 and column number 0 Is Sonata Software
Cell Value Of row number 3 and column number 1 Is Oct 7th 2014
Cell Value Of row number 3 and column number 2 Is 127
Cell Value Of row number 3 and column number 3 Is 153.05
Cell Value Of row number 3 and column number 4 Is 20.37
--------------------------------------------------
Number of cells In Row 4 are 5
Cell Value Of row number 4 and column number 0 Is IFCI
Cell Value Of row number 4 and column number 1 Is Sep 24th 2014
Cell Value Of row number 4 and column number 2 Is 35
Cell Value Of row number 4 and column number 3 Is 43.30
Cell Value Of row number 4 and column number 4 Is 25.51
--------------------------------------------------
Number of cells In Row 5 are 5
Cell Value Of row number 5 and column number 0 Is Minda Industries
Cell Value Of row number 5 and column number 1 Is Sep 15th 2014
Cell Value Of row number 5 and column number 2 Is 434
Cell Value Of row number 5 and column number 3 Is 655.00
Cell Value Of row number 5 and column number 4 Is 51.03
--------------------------------------------------
PASSED: Handle_Dynamic_Webtable
Some of the html tables which allows input on the website. Is there anyway how to insert a value into the html table at nth row and nth column using selenium web driver.
ReplyDeleteI accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.
ReplyDeleteselenium training in bangalore|