如何使用硒将具有相同标题的多个Web表写入相同的Excel工作表中?

时间:2019-05-01 06:18:47

标签: java excel selenium data-driven

我正在尝试将多个Web表(在每个页面上,即分页)写入具有相同标题的单个excel工作表中,并且能够写入9行第一张表,但对于第二个实例,索引需要从10开始。该怎么做?..下面是示例代码..任何帮助将不胜感激..谢谢您。

尽管我正在控制台上获得输出。

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

        }
    }
}    

预期输出应为每行1到9,并追加下一个实例输出,即1 2..9 1 2..9对接我对于第一个实例仅获得1到9。

0 个答案:

没有答案