Another concept that we commonly see in the web pages are the <table> </table>. for the user easiness and the maintainability most of the users use table tags inside the HTML codes. When reading the data inside the tables in web driver we have a concept called "Web Tables" where we dynamically select the rows and columns directly or in a loop.
Below set of examples will how how we can extract data from web tables in different scenarios.
Get the count of rows in a table
//Get the count of rows in the table
WebDriver A = new FirefoxDriver();
A.get("https://developers.google.com/bigquery/docs/sample-tables");
List<WebElement> URLS = A.findElements(By.xpath("//*[@id='gc-content']/div[1]/div/table/tbody/tr"));
int count = URLS.size();
System.out.println(count); // Prints 06 rows
Get the count of columns in a table
//Get the count of rows in the table
List<WebElement> URLS2 = A.findElements(By.xpath("//*[@id='gc-content']/div[1]/div/table/tbody/tr/td"));
int count2 = URLS2.size();
System.out.println(count2);
List<WebElement> URLS2 = A.findElements(By.xpath("//*[@id='gc-content']/div[1]/div/table/tbody/tr/td"));
int count2 = URLS2.size();
System.out.println(count2);
Get the values inside the table to a List
//Get the values inside the table to a List
List<WebElement> URLS = A.findElements(By.xpath("//*[@id='gc-content']/div[1]/div/table/tbody/tr"));
int count = URLS.size();
System.out.println(count);
for (int i = 0; i < URLS.size(); i++) {
System.out.println (URLS.get(i).getText()); }
Get the specific column value
//Print the specific column values
String Xpath1 = "//*[@id='gc-content']/div[1]/div/table/tbody/tr[";
String Xpath2 = "]/td[1]";
for (int i = 2; i <= count; i++) {
String CombineXpath = Xpath1+i+Xpath2;
String val = A.findElement(By.xpath(CombineXpath)).getText();
System.out.println(val); }
Print the specific row values
//Print the specific row values
String Xpath3 = "//*[@id='gc-content']/div[1]/div/table/tbody/tr[2]/td[";
String Xpath4 = "]";
for (int i = 1; i <= (count2/(count-1)); i++) {
String CombineXpath2 = Xpath3+i+Xpath4;
String val2 = A.findElement(By.xpath(CombineXpath2)).getText();
System.out.println(val2); }
Small calculation extracting table values
//Do a small calculation using the contents in the web table
String Xpath5 = "//*[@id='gc content']/div[1]/div/table/tbody/tr[";
String Xpath6 = "]/td[3]";
double xx = 0;
for (int i = 2; i <=3 ; i++) {
String Number_Val = A.findElement(By.xpath(Xpath5+i+Xpath6)).getText();
String Number_Val_Substring = Number_Val.substring(0,3);
double Number_Val_Converted = Double.parseDouble(Number_Val_Substring);
xx = xx + Number_Val_Converted; }
System.out.println(xx);
//Get the values inside the table to a List
List<WebElement> URLS = A.findElements(By.xpath("//*[@id='gc-content']/div[1]/div/table/tbody/tr"));
int count = URLS.size();
System.out.println(count);
for (int i = 0; i < URLS.size(); i++) {
System.out.println (URLS.get(i).getText()); }
Get the specific column value
//Print the specific column values
String Xpath1 = "//*[@id='gc-content']/div[1]/div/table/tbody/tr[";
String Xpath2 = "]/td[1]";
for (int i = 2; i <= count; i++) {
String CombineXpath = Xpath1+i+Xpath2;
String val = A.findElement(By.xpath(CombineXpath)).getText();
System.out.println(val); }
Print the specific row values
//Print the specific row values
String Xpath3 = "//*[@id='gc-content']/div[1]/div/table/tbody/tr[2]/td[";
String Xpath4 = "]";
for (int i = 1; i <= (count2/(count-1)); i++) {
String CombineXpath2 = Xpath3+i+Xpath4;
String val2 = A.findElement(By.xpath(CombineXpath2)).getText();
System.out.println(val2); }
Small calculation extracting table values
//Do a small calculation using the contents in the web table
String Xpath5 = "//*[@id='gc content']/div[1]/div/table/tbody/tr[";
String Xpath6 = "]/td[3]";
double xx = 0;
for (int i = 2; i <=3 ; i++) {
String Number_Val = A.findElement(By.xpath(Xpath5+i+Xpath6)).getText();
String Number_Val_Substring = Number_Val.substring(0,3);
double Number_Val_Converted = Double.parseDouble(Number_Val_Substring);
xx = xx + Number_Val_Converted; }
System.out.println(xx);
Hi,
ReplyDeleteThis blog i svery interesting at first observation...i hope i will get all answers for my questions, let see....many thanks for mataining this blog...
thanks,
Hii its nice explanation,precise and perfect hope to see more solutions from you.thanks
ReplyDelete