This requires some knowledge of HTML table. The table is the main tag where we have rows< tr >, and one row can have many columns < td >.
Sample table will be as given below. Write the code in notepad and save the file with any name and the extension should be html. Let’s say sample.html, open file in browser
< table border="1">
< tr>
< td>Java < /td>
< td>Pune< /td>
< /tr>
< tr>
< td>By< /td>
< td>Pune< /td>
< /tr>
< tr>
< td>Kiran< /td>
< td>Pune< /td>
< /tr>
< /table>
Output:
Java Pune
By Pune
Kiran Pune
The table needs to be automated, means we should be able to print all values in a table. In future we can test those values against expected values.
Now if we go about locating every element of a table, it’s not affordable as we don’t know how many rows will be there. Columns are fixed, but number of rows is not.
Need to write the same code again and again if dynamic code is not written.
There may be cases where we may not have any rows.
So we need to put some logic here as below:
First locate the table, and then check how many rows are there.
Iterate over rows. Find columns in every row. Iterate over columns and print data dynamically.
Complete code of above table:
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;
public class FirstExample {
public static void main(String[] args) {
// opens a Firefox browser
WebDriver driver = new FirefoxDriver()
// opening of url
driver.get("http://www.javabykiran.com/selenium/demo/");
// Maximize window driver.manage().window().maximize();
// locate email element
WebElement userEle = driver.findElement(By.cssSelector("input[id=email]"));
// enter uname into text box
userEle.sendKeys("kiran@gmail.com");
// locate password element
WebElement passwordEle = driver.findElement(By.id("password"));
// enter password into text box passwordEle.sendKeys("123456");
// locate sign in button
WebElement signInbuttonEle = driver.findElement(By.xpath(".//*[@ id='form']/div[3]/div/button"));
// click sign in button signInbuttonEle.click();
// locate users link
WebElement usersLink = driver.findElement(By.xpath("/html/body/div/aside[1]/section/ul/li[3]/a/span"));
// click users Link usersLink.click();
// locate table
WebElement Table = driver.findElement(By.xpath("//table[@ class='table table-hover']"));
// inside table go for rows.
List< WebElement > tr = Table.findElements(By.tagName("tr"));
// no of rows printed.
System.out.println("total no of rows " + tr.size());
// iterating rows.
for (WebElement row : tr) {
// get list of columns
List< WebElement > td = row.findElements(By.tagName("td"));
// iterating columns
for (WebElement col : td) {
// printing column text
System.out.println(col.getText());
}
System.out.println("...........");
}
// locate logout button
WebElement logoutButtonEle = driver.findElement(By.xpath("html/body/div/header/nav/div/ul/li/a"));
// click logout button logoutButtonEle.click();
}
}