我想在Excel中显示Webtable数据

时间:2019-05-08 17:37:19

标签: java selenium apache-poi

我希望它显示excel中的所有值,但不显示

driver.get(“ http://education-india.in/Education/Courses/?PageNumber=1”);

    WebDriverWait wait=new WebDriverWait(driver, 30);
    List<WebElement> dropdown =wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//select[@id='txtPageNumber']/option")));


    for(int k=1;k<dropdown.size()-1;k++) {
        List<WebElement> newdropdown =wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//select[@id='txtPageNumber']/option")));

        List<WebElement> rows = driver.findElements(By.xpath("//table[@class='index']/tbody/tr"));
       // List<WebElement> col = driver.findElements(By.xpath("//table[@class='index']/tbody/tr[1]/th"));

        for(int i=0;i<rows.size()-1;i++){
            WebElement webRow = rows.get(i);
            List<WebElement> col = webRow.findElements(By.tagName("td"));
            XSSFRow row = sheet.createRow(i);
            for (int j=0; j<col.size(); j++) {
                WebElement webCell = col.get(j);
                String text = webCell.getText();
                Cell excelCell = row.createCell(j);
                excelCell.setCellValue(webCell.getText());
            }


        }

        newdropdown.get(k).click();

    }

1 个答案:

答案 0 :(得分:1)

代码的问题是您每次都在重写行。您正在使用循环增量变量来创建另一个循环内的行。由于一页有20行,并且您要插入一页的行索引(从0到20),因此它会被最后一页覆盖。

在循环外声明一个rowCount变量。并在创建行后每次增加它。

尝试一下:

WebDriverWait wait=new WebDriverWait(driver, 30);
    List<WebElement> dropdown =wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//select[@id='txtPageNumber']/option")));

    int rowCount = 0;

    for(int k=1;k<dropdown.size()-1;k++) {
        List<WebElement> newdropdown =wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//select[@id='txtPageNumber']/option")));

        List<WebElement> rows = driver.findElements(By.xpath("//table[@class='index']/tbody/tr"));
       // List<WebElement> col = driver.findElements(By.xpath("//table[@class='index']/tbody/tr[1]/th"));

        for(int i=0;i<rows.size()-1;i++){
            WebElement webRow = rows.get(i);
            List<WebElement> col = webRow.findElements(By.tagName("td"));
            XSSFRow row = sheet.createRow(rowCount);
            rowCount++;
            for (int j=0; j<col.size(); j++) {
                WebElement webCell = col.get(j);
                String text = webCell.getText();
                Cell excelCell = row.createCell(j);
                excelCell.setCellValue(webCell.getText());
            }


        }

        newdropdown.get(k).click();

    }