In previous session we have seen how to handle web tables, in this script we see how dynamically get the tr and td's i.e rows and columns and display the content on the console.
Go to this site : http://www.w3schools.com/tags/tag_table.asp
and display all the information
First need to find the no. of rows by tag
Go to this site : http://www.w3schools.com/tags/tag_table.asp
and display all the information
First need to find the no. of rows by tag
and the columns by tag
Then get the size and loop through to print all the values in each cell.
Script :
Then get the size and loop through to print all the values in each cell.
Script :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.session5; | |
import java.util.List; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
public class WebTableDynamic { | |
public static void main(String[] args) throws InterruptedException { | |
// TODO Auto-generated method stub | |
System.setProperty("webdriver.chrome.driver","yourpath/chromedriver"); | |
WebDriver driver = new ChromeDriver(); | |
driver.get("http://www.w3schools.com/tags/tag_table.asp"); | |
Thread.sleep(8000); | |
WebElement table = driver.findElement(By.xpath("//*[@id='main']/table[2]/tbody")); | |
List<WebElement> rows = table.findElements(By.tagName("tr")); | |
System.out.println(rows.size()); | |
// to the col.. | |
for(int rownum=1;rownum<rows.size();rownum++){ | |
// how to get the col.. | |
List<WebElement> cells = rows.get(rownum).findElements(By.tagName("td")); | |
// fetch the col cells.. | |
for (int col=0;col<cells.size();col++){ | |
System.out.println(cells.get(col).getText()); | |
} | |
System.out.println("******"); | |
} | |
} | |
} |
Output :
align
left
center
right
Not supported in HTML5.
Specifies the alignment of a table according to surrounding text
3
bgcolor
rgb(x,x,x)
#xxxxxx
colorname
Not supported in HTML5.
Specifies the background color for a table
3
border
1
0
Specifies whether or not the table is being used for layout purposes
3
cellpadding
pixels
Not supported in HTML5.
Specifies the space between the cell wall and the cell content
3
cellspacing
pixels
Not supported in HTML5.
Specifies the space between cells
3
frame
void
above
below
hsides
lhs
rhs
vsides
box
border
Not supported in HTML5.
Specifies which parts of the outside borders that should be visible
3
rules
none
groups
rows
cols
all
Not supported in HTML5.
Specifies which parts of the inside borders that should be visible
3
sortable
sortable
Specifies that the table should be sortable
3
summary
text
Not supported in HTML5.
Specifies a summary of the content of a table
3
width
pixels
%
Not supported in HTML5.
Specifies the width of a table