It is nothing but a table in HTML where we can represent all data within a tabular form in web page.
Q. How we can understand that data represented in a tabular form ?
Ans : It is very easy to understand that if we can find that data is enclosed within <table> tag in DOM structure.
Q. How we can understand that data represented in a tabular form ?
Ans : It is very easy to understand that if we can find that data is enclosed within <table> tag in DOM structure.
Now we learn some HTML tags those are related with table:
<thead> : This tag represent the table header.
<th> : This tag denotes the header column.
<tfoot> : This tag represent the table footer
<tbody> : This tag means that main table body table data starts from here.
<tbody> : This tag means that main table body table data starts from here.
<td> : This tag is used to create table column .
<tr> : This tag is used to create table row.
Q. How we can access table using selenium webdriver ?
Ans : Concept is very simple at first we need to find the desire webtable from HTML page then we find the all Row elements.After getting the all row elements we find the all Column elements on each row element.
Now one thing keep in mind that table header column or cell and normal table column or cell represents in different way,for table header column represented by <th> tag and normal table column represent with <tr> tag.
So if we talk about the code then this is the way to handle webtable using Selenium WebDriver:
Getting TableBody :
driver.findElement(By.tagName("tbody"));
Or
driver.findElement(By.xpath("./tbody"));
Getting all Rows :
driver.findElements(By.tagName("tr"));
Or
driver.findElements(By.xpath("./tr"));
Getting all Columns or Cells From A Row :
driver.findElements(By.tagName("td"));
Or
driver.findElements(By.xpath("./td"));
Getting all Header Columns or Cells :
driver.findElements(By.tagName("th"));
Or
Ans : Concept is very simple at first we need to find the desire webtable from HTML page then we find the all Row elements.After getting the all row elements we find the all Column elements on each row element.
Now one thing keep in mind that table header column or cell and normal table column or cell represents in different way,for table header column represented by <th> tag and normal table column represent with <tr> tag.
So if we talk about the code then this is the way to handle webtable using Selenium WebDriver:
Getting TableBody :
driver.findElement(By.tagName("tbody"));
Or
driver.findElement(By.xpath("./tbody"));
Getting all Rows :
driver.findElements(By.tagName("tr"));
Or
driver.findElements(By.xpath("./tr"));
Getting all Columns or Cells From A Row :
driver.findElements(By.tagName("td"));
Or
driver.findElements(By.xpath("./td"));
Getting all Header Columns or Cells :
driver.findElements(By.tagName("th"));
Or
driver.findElements(By.xpath("./th"));
Sample Complete Code :
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class TableExample{
public static void main(String[] args) {
WebDriver driver = null;
try{
driver = new FirefoxDriver();
driver.get("https://en.wikipedia.org/wiki/List_of_largest_oil_and_gas_companies_by_revenue");
WebElement webTable =driver.findElement(By.xpath(".//*[@id='mw-content-text']/table/tbody/tr/td/table"));
/**
* Table header handling. Row denotes with <tr> tag and column denotes
* with <th> tag.
*/
WebElement webTableHeader = webTable.findElement(By.xpath("./thead"));
List<WebElement> webTableHeaderRows = webTableHeader.findElements(By.xpath("./tr"));
for (WebElement headerRow : webTableHeaderRows) {
List<WebElement> webTableHeaderColumns = headerRow.findElements(By.xpath("./th"));
System.out.println(" TableHeader Cell Text : ");
for (WebElement headerColumn : webTableHeaderColumns) {
System.out.print(headerColumn.getText() + "\t");
}
System.out.println("");
}
/**
* Table Body Handling. Row denotes with <tr> tag and column denotes
* with <td> tag.
*/
WebElement webTableBody = webTable.findElement(By.xpath("./tbody"));
List<WebElement> webTableBodyRows = webTableBody.findElements(By.xpath("./tr"));
System.out.println(" TableBody Cell Text : ");
for (WebElement tableBodyRow : webTableBodyRows) {
List<WebElement> webTableBodyColumns = tableBodyRow.findElements(By.xpath("./td"));
int column =0;
for (WebElement webTableBodyColumn : webTableBodyColumns) {
/**
* We can extract tool tip from first column or cell.
*/
if(column==0){
column++;
System.out.print(webTableBodyColumn.findElement(By.xpath(".//span/a/img")).getAttribute("alt") + "\t");
}
else{
System.out.print(webTableBodyColumn.getText() + "\t");
}
}
System.out.println("");
}
/**
* Table Footer Handling. Row denotes with <tr> tag and column denotes
* with <th> tag.
*/
WebElement webTableFooter = webTable.findElement(By.xpath("./tfoot"));
List<WebElement> webTableFooterRows = webTableFooter.findElements(By.xpath("./tr"));
for (WebElement webTablefooterRow : webTableFooterRows) {
List<WebElement> webTableFooterColumns = webTablefooterRow.findElements(By.xpath("./td"));
System.out.println(" TableFooter Cell Text : ");
for (WebElement webTableFooterColumn : webTableFooterColumns) {
System.out.print(webTableFooterColumn.getText() + "\t");
}
System.out.println("");
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally {
driver.quit();
}
}
}
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class TableExample{
public static void main(String[] args) {
WebDriver driver = null;
try{
driver = new FirefoxDriver();
driver.get("https://en.wikipedia.org/wiki/List_of_largest_oil_and_gas_companies_by_revenue");
WebElement webTable =driver.findElement(By.xpath(".//*[@id='mw-content-text']/table/tbody/tr/td/table"));
/**
* Table header handling. Row denotes with <tr> tag and column denotes
* with <th> tag.
*/
WebElement webTableHeader = webTable.findElement(By.xpath("./thead"));
List<WebElement> webTableHeaderRows = webTableHeader.findElements(By.xpath("./tr"));
for (WebElement headerRow : webTableHeaderRows) {
List<WebElement> webTableHeaderColumns = headerRow.findElements(By.xpath("./th"));
System.out.println(" TableHeader Cell Text : ");
for (WebElement headerColumn : webTableHeaderColumns) {
System.out.print(headerColumn.getText() + "\t");
}
System.out.println("");
}
/**
* Table Body Handling. Row denotes with <tr> tag and column denotes
* with <td> tag.
*/
WebElement webTableBody = webTable.findElement(By.xpath("./tbody"));
List<WebElement> webTableBodyRows = webTableBody.findElements(By.xpath("./tr"));
System.out.println(" TableBody Cell Text : ");
for (WebElement tableBodyRow : webTableBodyRows) {
List<WebElement> webTableBodyColumns = tableBodyRow.findElements(By.xpath("./td"));
int column =0;
for (WebElement webTableBodyColumn : webTableBodyColumns) {
/**
* We can extract tool tip from first column or cell.
*/
if(column==0){
column++;
System.out.print(webTableBodyColumn.findElement(By.xpath(".//span/a/img")).getAttribute("alt") + "\t");
}
else{
System.out.print(webTableBodyColumn.getText() + "\t");
}
}
System.out.println("");
}
/**
* Table Footer Handling. Row denotes with <tr> tag and column denotes
* with <th> tag.
*/
WebElement webTableFooter = webTable.findElement(By.xpath("./tfoot"));
List<WebElement> webTableFooterRows = webTableFooter.findElements(By.xpath("./tr"));
for (WebElement webTablefooterRow : webTableFooterRows) {
List<WebElement> webTableFooterColumns = webTablefooterRow.findElements(By.xpath("./td"));
System.out.println(" TableFooter Cell Text : ");
for (WebElement webTableFooterColumn : webTableFooterColumns) {
System.out.print(webTableFooterColumn.getText() + "\t");
}
System.out.println("");
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally {
driver.quit();
}
}
}
Nice blog about selenium webdriver.Thank you selenium training in chennai
ReplyDeleteselenium training in velachery
ReplyDeleteGet the most advanced RPA Course by RPA Professional expert. Just attend a FREE Demo session about how the RPA Tools get work.
For further details call us @ 9884412301 | 9600112302
RPA training in chennai | UiPath training in chennai