Handle Web Table in Web Driver

0


Table:-  it is the most important HTML container element which is used to display data using rows and columns.
We will use <table> to display elements in Table Format
<table>
<tr><td>rno</td><td>sname</td><td>branch</td><td>fees</td></tr>
<tr><td>1001</td><td>XYZ</td><td>CS</td><td>45000</td></tr>
<tr><td>1002</td><td>ABC</td><td>IT</td><td>65000</td></tr>
</table>
to locate table data we can use XPath
for example, if we want to find out the second row, the third column then we write the following path.
//table/tr[2]/td[2]
Create Script to display ABC text from Table?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TableExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("file:///D:/tabledemo.html");
WebElement ele = driver.findElement(By.xpath("//table/tbody/tr[3]/td[2]"));
System.out.print(ele.getText());
}
}
Handle Table Record using Predicate:-

It is used to access Nested Table data using table attributes. width,height etc.
Q)Example of Predicate to access Nested Table Data?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TableExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/newtours/");
String innerText = driver.findElement(By
.xpath("//table[@width=\"270\"]/tbody/tr[4]/td"))
.getText();
System.out.println(innerText);
}
}

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)