问题
i am trying to write multiple web table(on each page i.e pagination) into single excel sheet having same header..and able to write 9 rows of first table , but for second instance index need to start from 10, ..how to do that?..below are the sample code.. any help will be appreciated..thanks in advance..
though i am getting output on console..
public class DataScraper {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://etrain.info/in?PAGE=LIST--RAJ-TRAINS--1");
// *[@id="lowerdata"]/table/tbody/tr[1]/td/table/tbody/tr[5]/td[1]/a
// *[@id="lowerdata"]/table/tbody/tr[1]/td/table/tbody/tr[6]/td[1]/a
// *[@id="lowerdata"]/table/tbody/tr[1]/td/table/tbody/tr[29]/td[1]/a
String beforeXpath_TrainNoRow = "//td[@id='content']//tr[";
String afterXpath_TrainNoRow = "]//td[1]";
// div[@id='sublowerdata']//tr[3]//td[1]
// div[@id='sublowerdata']//tr[11]//td[1]
String beforeXpath_No = "//div[@id='sublowerdata']//tr[";
String afterXpath_No = "]//td[1]";
// int rowCount = TrainList.size();
Xls_Reader reader = new Xls_Reader(
"C:\\Selenium_Automation\\Projects\\DataDriven_FW\\src\\com\\testdata\\TrainSchedule.xlsx");
if (!reader.isSheetExist("Rajdhani")) {
reader.addSheet("Rajdhani");
reader.addColumn("Rajdhani", "IslNo");
// reader.addColumn("TrainSearch", "TrainName");
}
for (int i = 5; i <= 30; i++) {
String actualXpath_TrainNoRow = beforeXpath_TrainNoRow + i + afterXpath_TrainNoRow;
WebElement TrainNo = driver.findElement(By.xpath(actualXpath_TrainNoRow));
TrainNo.click();
Thread.sleep(5000);
List<WebElement> rows = driver.findElements(By.xpath("//table[@id='schtbl']//tr"));
System.out.println("total rows is " + rows.size());
int rowCount = rows.size();
for (int j = 3; j <= rowCount - 1; j++) {
String actualXpath_No = beforeXpath_No + j + afterXpath_No;
String SrNo = driver.findElement(By.xpath(actualXpath_No)).getText();
int islNo = Integer.parseInt(SrNo);
System.out.println(islNo);
reader.setCellData("Rajdhani", "IslNo", j, SrNo);
}
driver.navigate().back();
driver.navigate().refresh();
// table[@id='schtbl']//tr -- table rows inner
}
}
}
expected output should be from 1 to 9 in each rows appending next instance output i.e 1 2..9 1 2..9 butt i am getting only 1 to 9 for first instance.
来源:https://stackoverflow.com/questions/55932363/how-to-write-multiple-web-table-with-same-header-into-same-excel-sheet-using-sel