user1887967 user1887967 - 4 months ago 13
Java Question

get data from a table in a page with Java

I'm working in this project that opens a site pages fills the necesary fields get push the submit buttons and opens another window where some values are shown in a table, so far everything works ok but I don't know how to get the data from the table into my java program to save it in a file.

I'm working with Selenium WebDriver

this it's the code for my java program, I just need to get the info on the table:

import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.exec.util.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class Know_Your_Case_Status {

public void FillData(WebDriver driver) throws InterruptedException {

for (int i = 1; i < 12; i++) {

driver.get("http://cms.nic.in/ncdrcusersWeb/login.do?method=caseStatus");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//Select Radio Button for State Commission
driver.findElement(By.cssSelector("[id='UserType'][value='C']"))
.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// Now Select State Assam
WebElement SelectState = driver.findElement(By.name("state_id"));
Select Sl = new Select(SelectState);
Sl.selectByVisibleText("Assam");

/**** Select From Date ******/
driver.findElement(By.cssSelector("img.ui-datepicker-trigger"))
.click();

// Select may Month
Select SelMonth = new Select(driver.findElement(By
.className("ui-datepicker-month")));
SelMonth.selectByValue("4");

// Select Year 2015
Select SelYear = new Select(driver.findElement(By.id("scrollyear")));
SelYear.selectByVisibleText("2015");

// Select date 1
driver.findElement(By.linkText("1")).click();

/**** Select To Date ******/
// driver.findElement(By.cssSelector("img.ui-datepicker-trigger")).click();
driver.findElement(By.xpath("(//img[@alt='...'])[2]")).click();

// Select Jan Month
Select SelToMonth = new Select(driver.findElement(By
.className("ui-datepicker-month")));
SelToMonth.selectByVisibleText("January");

// Select Year 2016
Select SelToYear = new Select(driver.findElement(By
.id("scrollyear")));
SelToYear.selectByVisibleText("2016");

// Select date 1
driver.findElement(By.linkText("8")).click();

Select SelCaseType = new Select(driver.findElement(By
.name("condition")));
SelCaseType.selectByVisibleText("CaseType");

Select Selval = new Select(driver.findElement(By.name("ctId")));
Selval.selectByIndex(i+1);

//Selval.selectByValue("8");

driver.findElement(By.name("advs")).click();

}

}

}


And the code source from the page I'm trying to get the info it's:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Query Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="./stylesheet/confoStyle.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="javascript/CommonCDRC.js"></script>

<script language="javaScript">
function doPrint()
{
tbHead.style.display = "none";
tbTail1.style.display = "none";
tbTail2.style.display = "none";
tbMenu.style.display = "none";
window.print();

tbHead.style.display = "";
tbTail1.style.display = "";
tbTail2.style.display = "";
tbMenu.style.display = "";
}

function submitForm()
{
var flgSubmit = 1;

var dtOfHearing = document.loginForm.dtOfHearing.value;

if(flgSubmit == 1)
{

document.loginForm.method.value = "loadSelectloginForCLhtml";
document.loginForm.submit();
}
else
return false;

}

function loadProceeding(caseid, fano)
{
document.loginForm.fano.value=fano;
document.loginForm.cid.value=caseid;

document.loginForm.action="ViewProceedingCS.jsp";
document.loginForm.submit();
}
</script>
</head>
<body bgcolor="#9cd3ed">
<form name="loginForm" method="post" action="/ncdrcusersWeb/login.do" onsubmit="return submitForm()">
<input type="hidden" name="method" value="ViewProceedingCS">
<input type="hidden" name="fano" value="">
<input type="hidden" name="case_id_in" value="">
<input type="hidden" name="dtOfHearing" value="">
<input type="hidden" name="courtId" value="">
<input type="hidden" name="cid" value="">
<input type="hidden" name="stateCode" value="5">
<input type="hidden" name="distCode" value="0">



<table class="baseContaner topGroup" width="100%" align="center">








<tr>
<td width="3%" class="rptsubnumhd">SN</td>
<td width="10%" class="rptsubnumhd">Case No</td>
<td width="17%" class="rptsubtxthd">Complainant</td>
<td width="17%" class="rptsubtxthd">Respondent</td>
<td width="18%" class="rptsubtxthd">Complainant-Advocate</td>
<td width="17%" class="rptsubtxthd">Respondent-Advocate</td>
<td width="8%" class="rptsubnumhd">Date of Filing</td>
<td width="10%" class="rptsubnumhd">
Date of Next Hearing

</td>
</tr>




<tr>
<td class="rptnumhval">1</td>
<td class="rptnumhval"><a class="help" href="javascript:loadProceeding('5/0/A/19/2015','A/19/2015' )">
A/19/2015 </a></td>
<td class="rpttxthval">Managing Director, The Assam Power Distribution Company Ltd.</td>
<td class="rpttxthval">Smti Khaleda Begum</td>
<td class="rpttxthval">Mr. P. Bhowmick</td>
<td class="rpttxthval"></td>
<td class="rptnumhval">15/05/2015</td>
<td class="rptnumhval">&nbsp;

19/05/2015

</td>
</tr>


<tr>
<td class="rptnumhval">2</td>
<td class="rptnumhval"><a class="help" href="javascript:loadProceeding('5/0/A/20/2015','A/20/2015' )">
A/20/2015 </a></td>
<td class="rpttxthval">The Chief Regional Manager, United India Insurance Company Ltd.</td>
<td class="rpttxthval">Mr. Dulu Dey</td>
<td class="rpttxthval">Mr. R. Goswami</td>
<td class="rpttxthval"></td>
<td class="rptnumhval">20/05/2015</td>
<td class="rptnumhval">&nbsp;

26/05/2015

</td>
</tr>


<tr>
<td class="rptnumhval">3</td>
<td class="rptnumhval"><a class="help" href="javascript:loadProceeding('5/0/A/21/2015','A/21/2015' )">
A/21/2015 </a></td>
<td class="rpttxthval">Dr. Vijay Nahata</td>
<td class="rpttxthval">Smti. Moushumi Nath</td>
<td class="rpttxthval">Mr. N. Sharma</td>
<td class="rpttxthval">Ms. D. Tamuli</td>
<td class="rptnumhval">05/06/2015</td>
<td class="rptnumhval">&nbsp;

11/09/2015

</td>
</tr>


<tr>
<td class="rptnumhval">4</td>
<td class="rptnumhval"><a class="help" href="javascript:loadProceeding('5/0/A/22/2015','A/22/2015' )">
A/22/2015 </a></td>
<td class="rpttxthval">Sri Biren Kalita</td>
<td class="rpttxthval">The Branch Manager, Ulubari Branch (CDO-II), Oriental Insurance Co. Ltd.</td>
<td class="rpttxthval">Mr. A. K. Gupta</td>
<td class="rpttxthval">Mr. S. Bhuyan</td>
<td class="rptnumhval">15/06/2015</td>
<td class="rptnumhval">&nbsp;

21/09/2015

</td>
</tr>


<tr>
<td class="rptnumhval">5</td>
<td class="rptnumhval"><a class="help" href="javascript:loadProceeding('5/0/A/23/2015','A/23/2015' )">
A/23/2015 </a></td>
<td class="rpttxthval">Smti. Sabita Roy</td>
<td class="rpttxthval">Managing Director, Birla Sun Life Insurance Co. Ltd.</td>
<td class="rpttxthval">Mr. N. N. Karmakar</td>
<td class="rpttxthval"></td>
<td class="rptnumhval">15/06/2015</td>
<td class="rptnumhval">&nbsp;

07/09/2015

</td>
</tr>


<tr>
<td class="rptnumhval">6</td>
<td class="rptnumhval"><a class="help" href="javascript:loadProceeding('5/0/A/24/2015','A/24/2015' )">
A/24/2015 </a></td>
<td class="rpttxthval">Branch Manager, Indian Overseas Bank</td>
<td class="rpttxthval">Mr. Padmeswar Baruati</td>
<td class="rpttxthval">Mr. V.K.Dewan</td>
<td class="rpttxthval"></td>
<td class="rptnumhval">24/06/2015</td>
<td class="rptnumhval">&nbsp;

17/09/2015

</td>
</tr>



<tr>
<td class="topHdMsg" colspan="8"><input type=button value="Back" onClick="history.go(-1)"></td>
</tr>
</table>
</form>
</body>
</html>


I was thinking that maybe a driver.findElement(By.tagName("table")).getText() may work but I'm getting all the data in one line, is it a way to separete the head and the lines ?

Answer

You should try as below :-

List<WebElement> rows = driver.findElement(By.tagName("table")).findElements(By.tagName("tr"));

for(WebElement row : rows) 
 {
   List<WebElement> columns = row.findElements(By.tagName("td"));
    for(WebElement column : columns) 
      {
        String columnText = column.getText();     
       }
   }

Hope it helps...:)

Comments