MainMenu

Home Java Overview Maven Tutorials

Saturday 14 January 2017

Selenium code to get text from html table

For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


Hello friends,
Here is very simple code, which will print all the row data of a table of a webpage.
First of all will create a TestNG class, in before test annotation i will launch firefox & navigate to the URL.
In test annotation i will get table by Tag name("table"), then i will its row then its text & finally i will print that.

package jack;
import org.testng.annotations.Test;
import java.util.List;
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.BeforeTest;
public class Tableread
{
WebDriver driver;
@BeforeTest
public void beforeTest()
{
driver = new FirefoxDriver();
driver.navigate().to("http://only-testing-blog.blogspot.in/2013/09/test.html");
}
@Test
public void f()
{
List table1 = driver.findElements(By.tagName("table"));
System.out.println(table1.size());
List table = driver.findElements(By.tagName("tr"));
for(int i=0; i<table1.size(); i++)
{
WebElement table2 = table.get(i);
String table3 = table2.getText();
System.out.println(table3);
}
}
}


No comments:

Post a Comment