How to Fetch Number of Rows and Columns from a Dynamic WebTable
Posted By : Neha Dahiya | 31-Jul-2018
Static and Dynamic HTML tables are published on the web.
- Static tables: Type of table in which data is static i.e the number of rows and tables are fixed
- Dynamic Tables: Type of table in which data is
dynamic i.e number of rows and columns are not fixed. For example, on the basis of date filter number of rows get altered.
Using Selenium Web driver, the number of rows and columns can be predicted. The following can be found using this
- Number of columns and rows in a web table
- X row’s and Y column’s data
Static tables can be handled very easily but it is difficult to handle dynamic table as data in rows and columns is not constant.
Here is how you can fetch
import java.text.ParseException;
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 Noofrowsandcols {
public static void main(String[] args) throws ParseException {
WebDriver wd;
System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
wd= new ChromeDriver();
wd.get("http://money.rediff.com/gainers/bsc/dailygroupa?");
//No.of Columns
List col = wd.findElements(By.xpath(".//*[@id=\"leftcontainer\"]/table/thead/tr/th"));
System.out.println("No of cols are : " +col.size());
//No.of rows
List rows = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr/td[1]"));
System.out.println("No of rows are : " + rows.size());
wd.close();
}
}
Above code is explained as follows
Code Explanation:
- Declare Web Driver object as “wd” and initialized chrome driver
- Use List <WebElement> for
total number of columns in “col” - Use
findElement command to returnlist of all the elementswhich are matching to the specific locator - Using its X-path and findElement command get all the columns
- Repeat the above steps to get the total number of rows.
Thanks!!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Neha Dahiya
Neha is a bright QA Engineer with skills in manual testing . Apart from finding bugs in application, she loves sketching and painting.