Https://www.guru99.com/handling-dynamic-selenium-webdriver.html
Here is the types of HTML tables published on the web-
- static Tables: Data is Static i.e. number of rows and columns are fixed.
- Dynamic Tables: Data is Dynamic i.e. number of rows and columns was not fixed.
Below is an example of a dynamic table of Sales. Based on Input Date filters, number of rows would get altered. So, it's dynamic in nature.
Handling static table is easy, but the dynamic table is a little bit difficult as rows and columns was not constant.
In this tutorial, you'll learn-
Using X-path to Locate Web Table Elements
Example:fetch number of rows and columns from Dynamic webtable
Example:fetch cell value of a particular row and column of the Dynamic Table
Example:get Maximum of the Values in a Column of Dynamic Table
Example:get all the values of a Dynamic Table
Using X-path to Locate Web Table Elements
Before we locate Web element, first let's understands-
What is a Web element?
Web elements is nothing but HTML elements like TextBox, dropdowns radio buttons, submit buttons, etc. These HTML elements is written with the start tag and ends with an end tag.
For Example,
<p> My First HTML Document</p>.
Steps for getting x-path of the web element that we want to locate.
Step 1) In Chrome, Go to Http://money.rediff.com/gainers/bsc/daily/groupa
Step 2) Right click on the Web element whose x-path is to be fetched. In we case, right click on the "Company" select Inspect option. The following screen would be shown-
Step 3) Right Click on highlighted Web element > Select Copy, copy x-path option.
Step 4) Use the copied X-path "//*[@id =" Leftcontainer "]/table/thead/tr/th [1]" in Selenium webdriver to locate the element.
Example:fetch number of rows and columns from Dynamic webtable
When the table was dynamic in nature, we cannot predict its number of rows and columns.
Using Selenium Web Driver, we can find
- Number of Rows and columns of Web table
- X row or Y column ' s data.
Below is program for fetching total number of rows and columns of Web table.
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 is:" +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 (); }}
Code Explanation:
- Here we have the first declared Web Driver object "WD" &initialized it to Chrome Driver.
- We Use the List <WebElement> to total number of columns in "Col".
- findelements commands returns a list of all the elements matching the specified locator
- Using findelements and X-path//*[@id =\ "Leftcontainer\"]/table/thead/tr/th we get all the columns
- Similarly, we repeat the process for rows.
.
Output:
Example:fetch cell value of a particular row and column of the Dynamic Table
Let's assume we need 3rd row of the table and its second cell ' s data. See the table below-
In above table, data are regularly updated after some span of time. The data you try retrieve is different from the above screenshot. However, the code remains the same. Here's sample program to get the 3rd row and 2nd column ' s data.
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;import Java.util.concurrent.timeunit;public class RowandCell {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/daily/groupa?"); Wd.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Webelement basetable = wd.findelement (by.tagname ("table")); To find third row of table webelement TableRow = basetable.findelement (By.xpath ("//*[@id =\" Leftcontainer\ "]/table/ TBODY/TR[3]); String rowtext = Tablerow.gettext (); System.out.println ("Third row of table:" +rowtext); To get 3rd row ' s 2nd column data webelement cellineed = tablerow.findelement (By.xpath ("//*[@id =\" Leftcontainer\ "]/tab Le/tbody/tr[3]/td[2]); String valueineed = Cellineed.gettext (); System.out.println ("Cell value is:" + valueineed); Wd.close (); }}
Code Explanation:
- Table is located using the Locator property "TagName".
- Using XPath "//*[@id =\" Leftcontainer\ "]/table/tbody/tr[3]" find the 3rd row and gets its text using GetText () function
- Using Xpath "//*[@id =\" Leftcontainer\ "]/table/tbody/tr[3]/td[2]" find the 2nd cell in 3rd row and gets its text using Gett Ext () function
Output:
Example:get Maximum of the Values in a Column of Dynamic Table
In this example, we'll get the maximum of all values in a particular column.
Refer The following table-
Here is the code
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;import Java.text.numberformat;public class MaxFromTable {public static void M Ain (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/daily/groupa?"); String Max; Double m=0,r=0; No. of Columns List col = wd.findelements (By.xpath (".//*[@id = ' Leftcontainer ']/table/thead/tr/th")); System.out.println ("Total No of columns is:" +col.size ()); No.of rows List rows = wd.findelements (By.xpath (".//*[@id = ' Leftcontainer ']/table/tbody/tr/td[1]")); System.out.println ("Total No of rows is:" + rows.size ()); for (int i =1;i<rows.size (); i++) {max= wd.findelement (by.xpatH ("html/body/div[1]/div[5]/table/tbody/tr[" + (i+1) + "]/td[4]"). GetText (); NumberFormat f =numberformat.getnumberinstance (); Number num = F.parse (max); max = Num.tostring (); m = double.parsedouble (max); if (m>r) {r=m; }} System.out.println ("Maximum is:" + R); }}
Code Explanation:
- Using Chrome Driver We locate the Web table and get total number of row using XPath ".//*[@id = ' Leftcontainer ']/table/tbody /TR/TD[1] "
- Using for loops, we iterate through total number of rows and fetch the values one by one. To get next row we use (i+1) in XPath
- We compare old value with new value and maximum value are printed at the end of a for loop
OutPut
Example:get all the values of a Dynamic Table
Consider the following table http://demo.guru99.com/test/table.html
The number of columns for each row is different.
Here row number 1, 2 and 4 had 3 cells, and row number 3 had 2 cells, and row number 5 has 1 cell.
We need to get values of the cells
Here is the code:
Import Java.text.parseexception;import Java.util.list;import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Java.util.concurrent.timeunit;import Org.openqa.selenium.chrome.chromedriver;public class Nofrowscolmns {public static void main (string[] args) throws Pars eexception {Webdriver wd; System.setproperty ("Webdriver.chrome.driver", "G://chromedriver.exe"); WD = new Chromedriver (); Wd.manage (). Timeouts (). implicitlywait (5, timeunit.seconds); Wd.get ("http://demo.guru99.com/test/table.html"); to locate table. Webelement mytable = wd.findelement (By.xpath ("/html/body/table/tbody")); To locate rows of table. List < webelement > rows_table = mytable.findelements (By.tagname ("tr")); To calculate no of the rows in table. int rows_count = Rows_table.size (); Loop would execute till the last row of table. for (int row = 0; row < Rows_count; row++) {//to Locate columns (cells) ofthat specific row. List < webelement > columns_row = Rows_table.get (Row). Findelements (By.tagname ("TD")); To calculate no of columns (cells). In that specific row. int columns_count = Columns_row.size (); System.out.println ("Number of cells in Row" + Row + "is" + columns_count); Loop would execute till the last cell of this specific row. for (int column = 0, column < Columns_count; column++) {//To retrieve text from that specific cell. String Celtext = columns_row.get (column). GetText (); System.out.println ("Cell Value of row number" + Row + "and column number" + Column + "is" + celtext); } System.out.println ("--------------------------------------------------"); } }}
Code Explanation:
- Rows_count gives the total number of rows
- For each row, we get the total number of columns using Rows_table.get (ROW). Findelements (By.tagname ("TD"));
- We iterate through each column and of each row and fetch values.
Output:
Summary
- Static Web tables is consistent in nature. i.e. they do has fixed number of rows as well as Cell data.
- Dynamic Web tables is inconsistent i.e. they do not has fixed number of rows and cells data.
- Using Selenium Web driver, we can handle dynamic Web tables easily.
- Selenium Webdriver allows us to access dynamic Web tables by their X-path
The article is contributed by Kanchan Kulkarni.
[Selenium+java] Implicit wait & Explicit wait in Selenium