How To Extract Table Data/Read Table Data Using Selenium WebDriver Example

Table Is rattling often used chemical component In software spider web pages. Many times you lot postulate to extract your spider web tabular array data to compare too verify every bit per your seek out instance using selenium webdriver software testing tool. You tin read near How To Extracting All Links From Page If you lot postulate It In your seek out scenarios. If you lot knows, Selenium IDE software testing tool has also many commands related to Table and you lot tin thought all of them on LINK 1 too LINK 2 alongside examples. Now allow me come upward to our electrical flow tidings indicate near how to read values from table of software spider web application. Before reading values from spider web table, You must know at that topographic point are how many rows too how many columns In your table. Let nosotros endeavour to larn pose out of rows too columns from table.


How To Get Number Of Rows Using Selenium WebDriver

Look at the firebug thought of to a higher house HTML table. In whatsoever spider web table, Number of <tr> tags Inside <tbody> tag describes the pose out of rows of table. So you lot postulate to calculate that at that topographic point are how many <tr> tags Inside <tbody> tag. To larn row count for to a higher house given illustration table, nosotros tin purpose webdriver tilt similar bellow.

int Row_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr")).size();

In to a higher house syntax, .size() method alongside webdriver's findElements method volition yell back the count of <tr> tags too shop It In Row_count variable.

How To Get Number Of Columns Using Selenium WebDriver


Same agency In whatsoever spider web table, Number of <td> tags from whatsoever <tr> tag describes the pose out of columns of tabular array every bit shown In bellow given Image.


So for getting pose out of columns, nosotros tin purpose syntax similar bellow.
int Col_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[1]/td")).size();

Now nosotros accept row count too column count. One to a greater extent than affair you lot require to read information from tabular array Is generating Xpath for each prison theatre cellphone of column. Look at bellow given xpath syntax of 1st cell's 1st too 2nd rows.

//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[1]/td[1]
//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[2]/td[1]

Highlighted string Is mutual for both cell's xpath. Only changing Is row pose out Inside tr node. So hither nosotros tin pass Row_count variable's value  Inside tr node.

Same way, hold off at bellow given xpath syntax of 1st too 2nd cells of 1st row. Highlighted string Is mutual for both cell's xpath. Only changing Is column pose out Inside td node. So hither nosotros tin pass Col_count variable's value Inside td node.

//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[1]/td[1]
//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[1]/td[2]

All to a higher house affair Is applied In bellow given example. Simply run It In your eclipse using testng too break output In console.
package Testng_Pack;  import java.util.concurrent.TimeUnit;  import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  populace shape tabular array {     WebDriver driver = novel FirefoxDriver();  @BeforeTest     populace void setup() throws Exception {           driver.manage().window().maximize();          driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);          driver.get(" ");      }      @AfterTest  populace void tearDown() throws Exception {     driver.quit();      }      @Test  populace void print_data(){    //Get pose out of rows In table.  int Row_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr")).size();  System.out.println("Number Of Rows = "+Row_count);    //Get pose out of columns In table.  int Col_count = driver.findElements(By.xpath("//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[1]/td")).size();  System.out.println("Number Of Columns = "+Col_count);    //divided xpath In 3 parts to exceed Row_count too Col_count values.  String first_part = "//*[@id='post-body-6522850981930750493']/div[1]/table/tbody/tr[";  String second_part = "]/td[";  String third_part = "]";    //Used for loop for pose out of rows.  for (int i=1; i<=Row_count; i++){   //Used for loop for pose out of columns.   for(int j=1; j<=Col_count; j++){    //Prepared lastly xpath of specific prison theatre cellphone every bit per values of i too j.    String final_xpath = first_part+i+second_part+j+third_part;    //Will yell back value from located prison theatre cellphone too impress It.    String Table_data = driver.findElement(By.xpath(final_xpath)).getText();    System.out.print(Table_data +"  ");      }    System.out.println("");    System.out.println("");    }   } }

Console output of to a higher house given illustration volition looks similar bellow.
Number Of Rows = 3 Number Of Columns = vi xi  12  thirteen  fourteen  xv  xvi    21  22  23  24  25  26    31  32  33  34  35  36
Here you lot tin see, all the values of tabular array are extracted too printed.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM

http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post