{"id":3687,"date":"2020-06-11T19:40:43","date_gmt":"2020-06-11T14:10:43","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3687"},"modified":"2024-09-27T18:20:35","modified_gmt":"2024-09-27T12:50:35","slug":"handling-dynamic-web-tables-using-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/handling-dynamic-web-tables-using-selenium-webdriver\/","title":{"rendered":"Handling Dynamic Web Tables Using Selenium WebDriver"},"content":{"rendered":"\n<p>Web tables find their classification in two distinct forms: Static Web Tables and Dynamic Web Tables.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Static Web Tables:<\/strong> Here, constancy prevails. Rows and columns remain unchanging, maintaining their fixed count. Take, for instance, in a Table of Days or Months, nothing is expected to change.<\/li>\n\n\n\n<li><strong>Dynamic Web Tables:<\/strong> Here, rows and columns can change. For example, in a Student Table, an entity mirroring enrollment can change since enrolled students change yearly.<\/li>\n<\/ol>\n\n\n\n<p>Managing <a href=\"https:\/\/www.h2kinfosys.com\/blog\/how-to-handle-web-table-in-selenium-webdriver\/\">static web tables<\/a> is easier, whereas dealing with dynamic web tables presents a slight challenge due to the constant changes in their rows and columns.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Create Building Custom Functions for Webtables Selenium | Selenium Online Training | H2kinfosys\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/wTyK9-hANLE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Locating Web Table Elements<\/strong> with XPath<\/h2>\n\n\n\n<p>Before we go searching for web elements, let&#8217;s first get what a web element actually is.<\/p>\n\n\n\n<p>So, what&#8217;s a web element?<\/p>\n\n\n\n<p>A <a href=\"https:\/\/www.h2kinfosys.com\/blog\/finding-xpath-identify-web-elements\/\">web element<\/a> is basically an HTML element like radio buttons, dropdowns, textboxes, or submit buttons. These HTML things start with a tag and end with a tag.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<p>&lt;p&gt; My HTML Document&lt;\/p&gt;<\/p>\n\n\n\n<p>Steps to get <a href=\"https:\/\/www.h2kinfosys.com\/blog\/selenium-webdriver-xpath-complete-tutorial\/\">X-Path of web elements<\/a>. <\/p>\n\n\n\n<p><strong>Step 1: <\/strong>Open Google Chrome and type the URL https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa<\/p>\n\n\n\n<p><strong><img fetchpriority=\"high\" decoding=\"async\" width=\"602\" height=\"548\" alt=\"Stock.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/mxQ5XlPto2fZ-W97MWVe2rIBBobJdonDAa3R-qmBA2sUSrZtttR_790IeSv7s9UyxpCJOuaYP0J_OSjpWpYQqTL7gLBM5Y1wmdaZdrDTlmEnGOqyDqd7Jg-3XANFwt4F-nfKJNKBTe8S_01LOA\" title=\"\"><\/strong><\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Right-click on the web element labeled &#8220;Company,&#8221; the one you want to find the XPath for, and select &#8220;Inspect&#8221;.<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"602\" height=\"465\" alt=\"Company-Inspect.PNG\" src=\"https:\/\/lh4.googleusercontent.com\/TS6yXbE9aywemRJlbfAtgCb5PBBSBvfUZG_-jsbm_EBWGvgQzDHrvuMohEgKZ9gOB7LbLiqR_GZMIgZgUf67k2Y3n6L0CwPsezmvDgvAOQSuMfgYkp6tmxOt8ziewAf56GI7foXPpwwkWGnnIA\" title=\"\"><\/p>\n\n\n\n<p>Your screen will be as shown<\/p>\n\n\n\n<p><img decoding=\"async\" width=\"602\" height=\"225\" alt=\"Elements.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/aZ9Dnz6qDOryigOmZsgFlmHsOF8Y9S0_Fm9tTdu_gXXPymGUvHOkQIS_lqWs7Y8qWD-BHOFYoG3_EbWSMXIf-wFIqV_Zt-YR8YiddzL7fvEvYXSdY8FaJpqC0WiuqOsiAkVGZaTAQ_2ZfyVJ0Q\" title=\"\"><\/p>\n\n\n\n<p><strong>Step 3:<\/strong> Now, right-click on the highlighted web element for &#8220;Company,&#8221; choose &#8220;Copy,&#8221; and then click on &#8220;Copy XPath.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"372\" alt=\"Company.PNG\" src=\"https:\/\/lh4.googleusercontent.com\/guzF8QVBSjd8cIAlNfhc31LGs3I2PBFwrN3FqLV03a0wOdv3TflVBeQf9_MBUF4nmkX-i7_cRehXb-YQDlBtg5HPyk2WFCrGZDm91qmr9VkjgVVTwg7yMDVrQgOmyWBHZDUv99tcF4mpcUulcQ\" title=\"\"><\/p>\n\n\n\n<p><strong>Step 4: <\/strong>Use the copied XPath in Selenium WebDriver \u201c<strong>\/\/*[@id=&#8221;leftcontainer&#8221;]\/table\/thead\/tr\/th[1]\u201d<\/strong> to locate the element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example: How to Get the Count of Rows and Columns in a Dynamic WebTable<strong>.<\/strong><\/h2>\n\n\n\n<p>When a web table is dynamic, its exact number of rows and columns can&#8217;t be predicted in advance.<\/p>\n\n\n\n<p>With the help of Selenium WebDriver, we can determine:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The count of rows and columns in a web table.<\/li>\n\n\n\n<li>Data in a specific X row or Y column.<\/li>\n<\/ol>\n\n\n\n<p>The code snippet below helps to fetch the total count of rows and columns in a web table.<\/p>\n\n\n\n<p>Let&#8217;s create a test case in which we will automate the following scenarios to handle Web Table:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Invoke a Google chrome browser.<\/li>\n\n\n\n<li>Open URL: https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa<\/li>\n\n\n\n<li>Find total rows in a web table.<\/li>\n\n\n\n<li>Find a total no of columns in a web table.<\/li>\n<\/ul>\n\n\n\n<p>Now, we will create a test case step by step to understand how to handle Web Tables in WebDriver.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch Eclipse IDE.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Go to File menu &gt; New &gt; Click on Java Project.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"542\" alt=\"File.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/TwlsM_8oq4sK7EKEBEP71XdFjH1Eb8GDKmvegWt4QKTqekSqgF0oRFbGKyCwF1dF0xL3DFtYPTE6bs0zaSQKFvPGokA8BlbRu2yHipy-qnKHX5LZWlYBvzCQkB0MGrDtjZpSqk_Suj4ZBbVANg\" title=\"\"><\/p>\n\n\n\n<p><strong>Step 3:<\/strong> Right-click on the src folder and click on the New &gt; class.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"357\" alt=\"Classname.PNG\" src=\"https:\/\/lh3.googleusercontent.com\/hEBDwHjugsgK1viaD8qTdpP_5fSY95mvaHew0PCnd21ConN5F7UBY4GSdJ5t97BC5y-qiV-GHsUR3Y7PshwyxZMAfhWhmL4mLr4-6j3rNgTKFM5J_hnhn06QQKCQPBLT69V6vMu8z2WpcTLUbA\" title=\"\"><\/p>\n\n\n\n<p>Give the Class name as <strong>&#8220;NoofRowsandColumns&#8221; <\/strong>and Select the checkbox \u201cpublic static void main(String[] args) and click on the <strong>&#8220;Finish&#8221;<\/strong> button.<\/p>\n\n\n\n<p><strong>Step 4:<\/strong> Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.<\/p>\n\n\n\n<p>Here is the sample code to set Chrome driver system property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ System Property for Chrome Driver\u00a0\u00a0\u00a0\n\u00a0System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");<\/code><\/pre>\n\n\n\n<p>After that, we have to initialize the Chrome driver using ChromeDriver Class. Below is the sample code to initialize Chrome driver using ChromeDriver class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Instantiate a ChromeDriver class.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\nWebDriver driver=new ChromeDriver();<\/code><\/pre>\n\n\n\n<p>We will get the below code to launch Google Chrome browser after combining both of the above codes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");\u00a0\u00a0\nWebDriver driver=new ChromeDriver();\u00a0<\/code><\/pre>\n\n\n\n<p>After that, we need to navigate to the desired URL.<\/p>\n\n\n\n<p>Below is the sample code to navigate to the desired URL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Launch Website\u00a0\u00a0\ndriver.navigate().to(\"https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa. \");<\/code><\/pre>\n\n\n\n<p>Here is the complete code for above scenario:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.openqa.selenium.WebDriver;\u00a0\u00a0\nimport org.openqa.selenium.chrome.ChromeDriver;\u00a0\u00a0\n\u00a0\u00a0\npublic class WebTableHandle {\u00a0\u00a0\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0public static void main(String&#91;] args) {\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ System Property for Chrome Driver\u00a0\u00a0\u00a0\n\u00a0System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");\u00a0\u00a0\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Instantiate a ChromeDriver class.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0WebDriver driver=new ChromeDriver();\u00a0\u00a0\n\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Launch Website\u00a0\u00a0\ndriver.navigate().to(\"https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa. \");\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0\u00a0\u00a0\n}\u00a0<\/code><\/pre>\n\n\n\n<p><strong>Step 5:\u00a0 <\/strong>Find a total no of columns in a web table.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;WebElement>\u00a0 columns =\u00a0 driver.findElements(By.<em>xpath<\/em>(\"\/\/*&#91;@id=\u2019leftcontainer\u2019]\/table\/thead\/tr\/th\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>int<\/strong> columnCount = columns.size();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"No of columns in a table : \" + columnCount);<\/code><\/pre>\n\n\n\n<p><strong>Step 6:\u00a0 <\/strong>Find a total no of rows in a web table.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;WebElement>\u00a0 rows =\u00a0 driver.findElements(By.<em>xpath<\/em>(\"\/\/*&#91;@id=\u2019leftcontainer\u2019]\/table\/tbody\/tr\/td&#91;1]\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>int<\/strong> rowCount = rows.size();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"No of rows in a table : \" + rowCount);<\/code><\/pre>\n\n\n\n<p>Screenshot of the above scenario<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"360\" alt=\"Program.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/W_PKx5qyRW8s5fTkPGTPTl5U_l8CEuYRTrkC_rgcy7H4_bdTqNz1zUnh2GxAKu9xlzQ19CH8xBbsRgnG8OOElDonRvJVrbDRBvXanhQ0kI9RNSDCBTHpKMaeiKyaqVj2dw2FMhN7ikQzw7A1Uw\" title=\"\"><\/p>\n\n\n\n<p><strong>Now,\u00a0our\u00a0final\u00a0test\u00a0script\u00a0will\u00a0look\u00a0something\u00a0like:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> java.util.List;\n\n<strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n\n<strong>public<\/strong> <strong>class<\/strong> NoofRowsandColumns {\n\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\nSystem.<em>setProperty<\/em>(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\geckodriver.exe\");\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebDriver driver = <strong>new<\/strong> ChromeDriver();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0driver.get(\"https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa.\");\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\/\/No of Columns\nList&lt;WebElement>\u00a0 columns =\u00a0 driver.findElements(By.<em>xpath<\/em>(\"\/\/*&#91;@id='leftcontainer']\/table\/thead\/tr\/th\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>int<\/strong> columnCount = columns.size();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"No of columns in a table : \" + columnCount);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\/\/No of Rows\nList&lt;WebElement>\u00a0 rows =\u00a0 driver.findElements(By.<em>xpath<\/em>(\"\/\/*&#91;@id=\u2019leftcontainer\u2019]\/table\/tbody\/tr\/td&#91;1]\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>int<\/strong> rowCount = rows.size();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"No of rows in a table : \" + rowCount);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0driver.close();\n\n}\n\n}<\/code><\/pre>\n\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>At first we initialized chrome driver.<\/li>\n\n\n\n<li>Here we use List &lt;WebElement&gt; to total number of columns in &#8220;columns&#8221;.<\/li>\n\n\n\n<li>findElements commands will returns a list of ALL the elements matching the specified locator<\/li>\n\n\n\n<li>By using findElements and XPath &#8220;\/\/*[@id=&#8217;leftcontainer&#8217;]\/table\/thead\/tr\/th&#8221; we get all the columns<\/li>\n\n\n\n<li>We use List &lt;WebElement&gt; to total number of rows in &#8220;rows&#8221;.<\/li>\n\n\n\n<li>findElements commands will returns a list of ALL the elements matching the specified locator<\/li>\n\n\n\n<li>By using findElements and XPath &#8220;\/\/*[@id=\u2019leftcontainer\u2019]\/table\/tbody\/tr\/td[1]&#8221; we get all the rows.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example: Fetching cell value of a row and column of Dynamic Web Table<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s assume we need to fetch 2<sup>nd<\/sup>&nbsp;row of the table and its second cell data of the table below-<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"458\" alt=\"example.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/cxjCOCS5hkP_wuI4d9rCVzU3AsIserDvYu85D7vZoQx76Mqvs4uhkwojtBJ77jGjRXBx2pstVt3OgUkuVGuti2TSPU19pMOktnE0q2vp1bAvEwPOyg5cJDJpKaVIIm7txCSsfSejREM-iRCIMA\" title=\"\"><\/p>\n\n\n\n<p><strong>Here is a sample code to get the 2<\/strong><strong><sup>nd<\/sup><\/strong><strong>&nbsp;row and 2<\/strong><strong><sup>nd<\/sup><\/strong><strong>&nbsp;column&#8217;s data.<\/strong><\/p>\n\n\n\n<p>Consider the below program to fetch the total number of rows and columns of web table<\/p>\n\n\n\n<p>Let&#8217;s create a test case in which we will automate the following scenarios to handle Web Table:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Invoke a Google chrome browser.<\/li>\n\n\n\n<li>Open URL: https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa<\/li>\n\n\n\n<li>Find the second row of a table<\/li>\n\n\n\n<li>Find the table\u2019s 2<sup>nd<\/sup> row and 2<sup>nd<\/sup> cell data.<\/li>\n\n\n\n<li>Close the driver<\/li>\n<\/ul>\n\n\n\n<p>Now, we will create a test case step by step to understand how to handle the above scenario in WebDriver.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch Eclipse IDE.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Go to File &gt; New &gt; Click on Java Project.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"538\" height=\"542\" alt=\"File.PNG\" src=\"https:\/\/lh6.googleusercontent.com\/TwlsM_8oq4sK7EKEBEP71XdFjH1Eb8GDKmvegWt4QKTqekSqgF0oRFbGKyCwF1dF0xL3DFtYPTE6bs0zaSQKFvPGokA8BlbRu2yHipy-qnKHX5LZWlYBvzCQkB0MGrDtjZpSqk_Suj4ZBbVANg\" title=\"\"><\/p>\n\n\n\n<p><strong>Step 3:<\/strong> Right-click on the src folder and click on the New &gt; class.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"357\" alt=\"Classname.PNG\" src=\"https:\/\/lh3.googleusercontent.com\/hEBDwHjugsgK1viaD8qTdpP_5fSY95mvaHew0PCnd21ConN5F7UBY4GSdJ5t97BC5y-qiV-GHsUR3Y7PshwyxZMAfhWhmL4mLr4-6j3rNgTKFM5J_hnhn06QQKCQPBLT69V6vMu8z2WpcTLUbA\" title=\"\"><\/p>\n\n\n\n<p>Give the Class name as <strong>&#8220;RowdataandCell&#8221; <\/strong>and Select the checkbox \u201cpublic static void main(String[] args) and click on the <strong>&#8220;Finish&#8221;<\/strong> button.<\/p>\n\n\n\n<p><strong>Step 4:<\/strong> Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.<\/p>\n\n\n\n<p>Here is the sample code to set Chrome driver system property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ System Property for Chrome Driver\u00a0\u00a0\u00a0\n\u00a0System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");<\/code><\/pre>\n\n\n\n<p>After that, we have to initialize the Chrome driver using ChromeDriver Class. Below is the sample code to initialize Chrome driver using ChromeDriver class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Instantiate a ChromeDriver class.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\nWebDriver driver=new ChromeDriver();\u00a0<\/code><\/pre>\n\n\n\n<p>We will get the below code to launch Google Chrome browser after combining both of the above codes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");\u00a0\u00a0\nWebDriver driver=new ChromeDriver();<\/code><\/pre>\n\n\n\n<p>After that, we need to navigate to the desired URL.<\/p>\n\n\n\n<p>Below is the sample code to navigate to the desired URL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Launch Website\u00a0\u00a0\ndriver.navigate().to(\"https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa \");<\/code><\/pre>\n\n\n\n<p>Here is the complete code for above scenario:<\/p>\n\n\n\n<p><strong>Step 5:<\/strong> To find second row of a table<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"91\" alt=\"secondrow.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/RMuF0awkFkosICZ3bHLiu00IR7Fif7IeITOdvgkHEbB4083W8h3RpQFweXD-KfZUbrCSOwxBTZa4MOVFAjipw0E9Ft22H9_xVNOpjFWrR7RO6jTcdNgMuOycmFESp8E2nfntRJ1roW874O8Ryg\" title=\"\"><\/p>\n\n\n\n<p><strong>Step 6: <\/strong>Find tables 2<sup>nd<\/sup> row and 2<sup>nd<\/sup> cell data.<\/p>\n\n\n\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"79\" alt=\"2nd.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/i4IJ9va42I2ZQ1TZyR_SFEZWw3_RI-dBGaMWGySNZ6Vx9GH4hBPMcUvvipNr22xVS16TGDYRyVfzs4bdXHHTGdhq7mdmAIFiihgFfrhWveKraZeYwYnyUetjUsSX6I66UMtwSy_ADSH3UU2n6A\" title=\"\"><\/strong><\/p>\n\n\n\n<p><strong>Now,\u00a0our\u00a0final\u00a0test\u00a0script\u00a0will\u00a0look\u00a0something\u00a0like:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n\n<strong>public<\/strong> <strong>class<\/strong> RowdataandCell {\n\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\nSystem.<em>setProperty<\/em>(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\geckodriver.exe\");\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebDriver driver = <strong>new<\/strong> ChromeDriver();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0driver.get(\"https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa.\");\nWebElement baseTable = driver.findElement(By.<em>tagName<\/em>(\"table\"));\n\u00a0\n\/\/To find the second row of a table\nWebElement Rowtable = baseTable.findElement(By.<em>xpath<\/em>(\"\/\/*&#91;@id=\\'leftcontainer\\']\/table\/tbody\/tr&#91;2]\"));\nString rowtext = Rowtable.getText();\nSystem.<strong><em>out<\/em><\/strong>.println(\"The row text of the web table : \"+rowtext);\n\n\/\/To get 2nd row's 2nd column data\nWebElement cellNo = Rowtable.findElement(By.<em>xpath<\/em>(\"\/\/*&#91;@id=\\'leftcontainer\\']\/table\/tbody\/tr&#91;2]\/td&#91;2]\"));\nString valueNo = cellNo.getText();\nSystem.<strong><em>out<\/em><\/strong>.println(\"Cell value is : \"+valueNo);\ndriver.close();\n}\n}<\/code><\/pre>\n\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using locator property &#8220;tagname&#8221; the table is located.<\/li>\n\n\n\n<li>Using the XPath &#8220;\/\/*[@id=\\&#8221;leftcontainer\\&#8221;]\/table\/tbody\/tr[2]&#8221; find the 2nd row and gets its text using getText () function<\/li>\n\n\n\n<li>Using the XPath &#8220;\/\/*[@id=\\&#8221;leftcontainer\\&#8221;]\/table\/tbody\/tr[2]\/td[2]&#8221; find the 2nd cell in 2<sup>nd <\/sup>row and gets its text using getText () function<\/li>\n<\/ul>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"123\" alt=\"output.PNG\" src=\"https:\/\/lh5.googleusercontent.com\/uh9z8qfyPhsSz2--637fhQIGqWxwgBfH4FQYllV3FpHPbKR_gCSKs1ZW3Fbr70ov53I3198CzaucnKu_pOimr_oiaeBpkSILBpyBc1js3LLlUjkbvFHWcOxLSJUNffuddPW48rgfuRRgWI-mXQ\" title=\"\"><\/strong><\/p>\n\n\n\n<p><strong>Example: Getting Maximum of all the Values in a Column of Dynamic Web Table<\/strong><\/p>\n\n\n\n<p>Consider the below example, to get the maximum of all values in a particular column.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"530\" src=\"https:\/\/lh4.googleusercontent.com\/cqoV8N2OWqL3dUc66Krl92JNmV4zddbw4E8LPg138_ZsUH6jYfWf9wAZLCkq_vas4JBVhmbJMawfLQe-a20M3fZFl1vrV821uNNmQP9b5dqMNvlOURgY-LnGr4BV31A1aXNPSdeULZcOODti7g\" alt=\"Total.PNG\" title=\"\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> java.text.NumberFormat;\n<strong>import<\/strong> java.text.ParseException;\n<strong>import<\/strong> java.util.List;\n\n<strong>import<\/strong> org.openqa.selenium.By;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.WebElement;\n<strong>import<\/strong> org.openqa.selenium.chrome.ChromeDriver;\n\n<strong>public<\/strong> <strong>class<\/strong> maximumvaluesfromtable {\n\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) <strong>throws<\/strong> ParseException {\n\/\/ <strong>TODO<\/strong> Auto-generated method stub\nSystem.<em>setProperty<\/em>(\"webdriver.chrome.driver\", \"D:\\\\Drivers\\\\geckodriver.exe\");\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0WebDriver driver = <strong>new<\/strong> ChromeDriver();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0driver.get(\"https:\/\/money.rediff.com\/gainers\/bsc\/daily\/groupa.\");\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String max;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>double<\/strong> n=0, p=0;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/No of Columns\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0List&lt;WebElement>\u00a0 columns = driver.findElements(By.<em>xpath<\/em>(\".\/\/*&#91;@id='leftcontainer']\/table\/thead\/tr\/th\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"No of columns : \" +columns.size());\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/No of Rows\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0List&lt;WebElement>\u00a0 rows = driver.findElements(By.<em>xpath<\/em>(\".\/\/*&#91;@id='leftcontainer']\/table\/tbody\/tr\/td&#91;1]\"));\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"No of rows : \" +rows.size());\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>for<\/strong> (<strong>int<\/strong> i =1;i&lt;rows.size();i++)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0max= driver.findElement(By.<em>xpath<\/em>(\"html\/body\/div&#91;1]\/div&#91;5]\/table\/tbody\/tr&#91;\" + (i+1)+ \"]\/td&#91;4]\")).getText();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0NumberFormat f =NumberFormat.<em>getNumberInstance<\/em>();\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Number num = f.parse(max);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0max = num.toString();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0n = Double.<em>parseDouble<\/em>(max);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<strong>if<\/strong>(n>p)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0p=n;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"The web table current price : \"+ p);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\n}\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Explanation of Code:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We get total number of rows using XPath &#8220;.\/\/*[@id=&#8217;leftcontainer&#8217;]\/table\/tbody\/tr\/td[1]&#8221;&nbsp;<\/li>\n\n\n\n<li>Using for loop, we iterate through the total number of rows and fetch the values. To get next row we use (i+1) in XPath<\/li>\n\n\n\n<li>We will compare the old value with new value and the maximum value is get printed at the end of for loop.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion:<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To access the table elements we use By.xpath()<\/li>\n\n\n\n<li>Static web tables are consistent which means it has a fixed number of rows and cell data&nbsp;&nbsp;<\/li>\n\n\n\n<li>Dynamic web table is inconsistent which means the number of rows and columns will be dynamically changing, and it keeps on increasing or decreasing based on data.<\/li>\n\n\n\n<li>We access dynamic web tables with their X-path in Selenium Webdriver.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-webdriver-junit-training-course\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"90\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Advanced-Training-1.jpeg\" alt=\"Selenium Advanced Training\" class=\"wp-image-6623\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Advanced-Training-1.jpeg 728w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/11\/Selenium-Advanced-Training-1-300x37.jpeg 300w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Web tables find their classification in two distinct forms: Static Web Tables and Dynamic Web Tables. Managing static web tables is easier, whereas dealing with dynamic web tables presents a slight challenge due to the constant changes in their rows and columns. Locating Web Table Elements with XPath Before we go searching for web elements, [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3692,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[966,964,967,448,965,1837],"class_list":["post-3687","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-dynamic-web-table","tag-dynamic-web-tables","tag-locate-web-table-elements","tag-selenium-webdriver","tag-static-web-table","tag-web-elements"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3687"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3687\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3692"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}