- import java.util.Iterator;
- import java.util.List;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class ReadingTableExample {
- private WebDriver driver;
- private String baseUrl="http://www.espncricinfo.com/";
- @Before
- public void setUp() throws Exception {
- driver = new FirefoxDriver();
- driver.get(baseUrl);
- }
- //Test to display how to read html table using webdriver on cricinfo.com.
- @Test
- public void printFacebookFriendList() throws Exception {
- //Get all the links for Scorecard.
- WebElement box = driver.findElement(By.cssSelector("div.ciHomeTopHeadlines"));
- List <WebElement> scorecard = box.findElements(By.linkText("Scorecard"));
- //Click on the first scorecard link from News Section
- (scorecard.get(0)).click();
- //Get all the data of the table
- WebElement table =
- driver.findElement(By.id("inningsBat1"));
- List<WebElement> rows = table.findElements(By.tagName("tr"));
- Iterator<WebElement> i = rows.iterator();
- //Print the table.
- while(i.hasNext()) {
- WebElement row = i.next();
- List<WebElement> columns = row.findElements(By.tagName("td"));
- Iterator<WebElement> j = columns.iterator();
- while(j.hasNext()) {
- WebElement column = j.next();
- //Removing blank columns data and add a separator while displaying data.
- if (!column.getText().trim().equals("")){
- System.out.print(column.getText());
- System.out.print(" | ");
- }
- }
- System.out.println("");
- System.out.println("-----------------------------------------------");
- }
- }
- @After
- public void tearDown() throws Exception {
- driver.quit();
- }
- }