7. Working with WebTables
------------------------------------
WebTable elements are usually located under “tr” and “td” tags
tr -->is a Table row
number of “tr” tags count equals number of Rows in a Table
td-->table data/cell data
number of “td” tags count equals number of columns in a Table
th -->is a Table header
using "getText()" method we can use to read cell value
Exp: td.getText() will read cell value
Exp: Write script to read specific cell value from webtable
Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.w3schools.com/");
driver.manage().window().maximize();
//to click on "Learn HTML"
driver.findElement(By.linkText("Learn HTML")).click();
//to click on "HTML Tables"
driver.findElement(By.linkText("HTML Tables")).click();
//to read cell data
String cellData=driver.findElement(By.xpath("//table[@id='customers']/tbody/tr[3]/td[2]")).getText();
System.out.println(cellData);
Exp: Write script to read all the cell values from webtable
Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.w3schools.com/");
driver.manage().window().maximize();
//to click on "Learn HTML"
driver.findElement(By.linkText("Learn HTML")).click();
//to click on "HTML Tables"
driver.findElement(By.linkText("HTML Tables")).click();
//create reference object for WebTable
WebElement myTable=driver.findElement(By.id("customers"));
//to get all the "td" tags within the table
List tds=myTable.findElements(By.tagName("td"));
System.out.println("Number of td tags are: "+tds.size());
//to read each td/cell data
for(WebElement td: tds){
System.out.println(td.getText());
}
Ex: Write script to find number of Rows, number of columns in each row, and read cell value from Webtable
Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.w3schools.com/");
driver.manage().window().maximize();
//to click on "Learn HTML"
driver.findElement(By.linkText("Learn HTML")).click();
//to click on "HTML Tables"
driver.findElement(By.linkText("HTML Tables")).click();
//create reference object for WebTable
WebElement myTable=driver.findElement(By.id("customers"));
//to find number of Rows
List t_rows=myTable.findElements(By.tagName("tr"));
System.out.println("Number of Rows are: "+t_rows.size());
//to find each row number of td tags
for(WebElement t_row: t_rows){
List tds= t_row.findElements(By.tagName("td"));
System.out.println("Number of td tags are: "+tds.size());
for(WebElement td: tds){
System.out.println(td.getText());
}
System.out.println("***************************");
}
------------------------------------
WebTable elements are usually located under “tr” and “td” tags
tr -->is a Table row
number of “tr” tags count equals number of Rows in a Table
td-->table data/cell data
number of “td” tags count equals number of columns in a Table
th -->is a Table header
using "getText()" method we can use to read cell value
Exp: td.getText() will read cell value
Exp: Write script to read specific cell value from webtable
Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.w3schools.com/");
driver.manage().window().maximize();
//to click on "Learn HTML"
driver.findElement(By.linkText("Learn HTML")).click();
//to click on "HTML Tables"
driver.findElement(By.linkText("HTML Tables")).click();
//to read cell data
String cellData=driver.findElement(By.xpath("//table[@id='customers']/tbody/tr[3]/td[2]")).getText();
System.out.println(cellData);
Exp: Write script to read all the cell values from webtable
Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.w3schools.com/");
driver.manage().window().maximize();
//to click on "Learn HTML"
driver.findElement(By.linkText("Learn HTML")).click();
//to click on "HTML Tables"
driver.findElement(By.linkText("HTML Tables")).click();
//create reference object for WebTable
WebElement myTable=driver.findElement(By.id("customers"));
//to get all the "td" tags within the table
List
System.out.println("Number of td tags are: "+tds.size());
//to read each td/cell data
for(WebElement td: tds){
System.out.println(td.getText());
}
Ex: Write script to find number of Rows, number of columns in each row, and read cell value from Webtable
Script:
WebDriver driver= new FirefoxDriver();
driver.get("https://www.w3schools.com/");
driver.manage().window().maximize();
//to click on "Learn HTML"
driver.findElement(By.linkText("Learn HTML")).click();
//to click on "HTML Tables"
driver.findElement(By.linkText("HTML Tables")).click();
//create reference object for WebTable
WebElement myTable=driver.findElement(By.id("customers"));
//to find number of Rows
List
System.out.println("Number of Rows are: "+t_rows.size());
//to find each row number of td tags
for(WebElement t_row: t_rows){
List
System.out.println("Number of td tags are: "+tds.size());
for(WebElement td: tds){
System.out.println(td.getText());
}
System.out.println("***************************");
}
No comments:
Post a Comment