简体   繁体   English

如何使用WebDriver将副标题数据与Web上的边表数据一起打印到excel

[英]How to print column headers along side table data from web to excel with webdriver

Issue The below code allows me to either print column headers or print web table data into a csv file depending on if I chose 'th' or 'td' tags but not both simultaneously. 问题下面的代码允许我将列标题打印或将Web表数据打印到csv文件中,具体取决于我选择的是“ th”标签还是“ td”标签,但不能同时选择两者。

My Question How do I get it to print to csv both the 'th' and 'td' text at the same time in my CSV output?? 我的问题如何在CSV输出中同时打印“ th”和“ td”文本?

Code I have tried 2 versions of my code, but results are same.Both versions attached. 代码我尝试了2个版本的代码,但结果相同。两个版本均附上。

Code Version 1 代码版本1

public class WebToCSV {

static WebDriver driver = new FirefoxDriver();

    public static void main(String[] args) throws Throwable   {

        //driver.navigate().to("http://goo.gl/El1PIV");
        driver.navigate().to("http://www.bloomberg.com/markets/stocks/futures");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        WebElement table = driver.findElement(By.cssSelector
                ("div[class='data-tables first']"));

        List<WebElement> irow = table.findElements
                (By.cssSelector("div[class='data-tables first']  tr"));
        System.out.println("No. of rows in the table are: " + irow.size());

        // Create excel workbook and sheet.
        FileOutputStream fos = new FileOutputStream
                ("/Users/HARSHENDU/Desktop/Selenium_Practice/Stats.csv");

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet ws = wb.createSheet("WriteToXL");

        for(int r=0; r<irow.size(); r++) {
            WebElement webRow = irow.get(r);
            System.out.println(webRow.getText());
            XSSFRow row = ws.createRow(r);
            List<WebElement> allCells = webRow.findElements(By.tagName("td"));

            for(int c=0; c<allCells.size(); c++) {
                WebElement webCell = allCells.get(c);
                String text = webCell.getText();

                XSSFCell excelCell = row.createCell(c);
                excelCell.setCellValue(text);
            }

            System.out.println("");
        }

        fos.flush();
        wb.write(fos);
        fos.close();

        end();

}
    public static void end() {
        driver.close();
        driver.quit();
    }

} }

Code Version 2 This version has 2 sets of for loops, first one to print column headers in csv, and second one to print all data from the webtable. 代码版本2此版本有2套for循环,第一套用于打印csv中的列标题,第二套用于打印来自web表的所有数据。

public class StatsToxL {

static WebDriver driver = new FirefoxDriver();

public static void main(String[] args) throws Throwable   {

    //driver.navigate().to("http://goo.gl/El1PIV");
    driver.navigate().to("http://www.bloomberg.com/markets/stocks/futures");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    WebElement table = driver.findElement(By.cssSelector
            ("div[class='data-tables first']"));

    // Get webtable header column row.
    List<WebElement> irow2 = table.findElements
            (By.cssSelector("div[class='data-tables first'] thead tr "));
    System.out.println("No. of rows in the header are: " + irow2.size());

    // Get all webtable rows
    List<WebElement> irow = table.findElements
            (By.cssSelector("div[class='data-tables first'] tbody tr"));
    int iRowCount = irow.size();
    System.out.println("No. of rows in the table are: " + iRowCount);

    // Create excel workbook and sheet.
    FileOutputStream fos = new FileOutputStream
            ("/Users/HARSHENDU/Desktop/Selenium_Practice/Stats.csv");

    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet ws = wb.createSheet("WriteToXL");

    // Iterate over webtable header row and header cells.
    for(int r2=0; r2<irow2.size(); r2++) {
    WebElement webRow2 = irow2.get(r2);
        System.out.println(webRow2.getText());
        XSSFRow row2 = ws.createRow(r2);

        List<WebElement> allCells2 = webRow2.findElements(By.tagName("th"));

        for(int c2=0; c2<allCells2.size(); c2++) {
            WebElement webCell = allCells2.get(c2);
            String text2 = webCell.getText();
            XSSFCell excelCell2 = row2.createCell(c2);
            excelCell2.setCellValue(text2);

        }
        System.out.println("");
        fos.flush();
        wb.write(fos);
    }

    // Iterate over webtable rows and cells.
    for(int r=0; r<iRowCount; r++) {
        WebElement webRow = irow.get(r);
        System.out.println(webRow.getText());
        XSSFRow row = ws.createRow(r);
        List<WebElement> allCells = webRow.findElements(By.tagName("td"));

        for(int c=0; c<allCells.size(); c++) {
            WebElement webCell = allCells.get(c);
            String text = webCell.getText();

            XSSFCell excelCell = row.createCell(c);
            excelCell.setCellValue(text);
        }

        System.out.println("");
    }

    fos.flush();
    wb.write(fos);
    fos.close();

    end();

}
public static void end() {
    driver.close();
    driver.quit();
}

}

Change 更改

webRow.findElements(By.tagName("td"))

to

webRow.findElements(By.xpath("//td | //th"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM