Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Table:-  it is the most important HTML container element which is used to display data using rows and column.

We will use <table> to display element 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, 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 attribute. 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

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post