我在这个项目中工作,打开一个网站页面填充必要的字段按下提交按钮并打开另一个窗口,其中一些值显示在表格中,到目前为止一切正常但我没有'知道如何将表中的数据存入我的java程序,以将其保存在文件中。
我正在使用Selenium WebDriver
这是我的java程序的代码,我只需要获取表格中的信息:
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();
}
}
}
来自网页的代码来源我试图获取信息:
<!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">
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">
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">
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">
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">
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">
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>
我在想,也许是一个driver.findElement(By.tagName(&#34; table&#34;))。getText()可能有效,但我将所有数据都放在一行,是不是如何分割头部和线条?
答案 0 :(得分:1)
你应该尝试如下: -
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();
}
}
希望它有帮助...:)