简体   繁体   English

使用硒使用Apache POI写入Excel文件

[英]Write to excel file using apache poi using selenium

Please before you down vote this, I was not able to find an example of reading a web table and writing it to an Excel file. 请在您对此投反对票之前,我无法找到读取网络表并将其写入Excel文件的示例。 If you happen to find that link kindly provide it. 如果您偶然发现该链接,请提供它。 I have found plenty of examples on how to write to an Excel file but without reading from web table part. 我已经找到了很多有关如何写入Excel文件的示例,但是没有从Web表中读取内容。

Here is my code: 这是我的代码:

public class WebTable1 {

    public static void main(String[] args) throws IOException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("https://www.w3schools.com/html/html_tables.asp");

        //tr means Row, this table has 7 rows including Header
        ///td means Column, this table has 3 columns

        //*[@id="customers"]/tbody/tr[2]/td[1]
        //*[@id="customers"]/tbody/tr[7]/td[1]
        //Notice above the pattern, only the values are changing for tr[??]- which is why we will break it down into 2 String
        //below and then concatinate them as String

        String beforeXpath_Company = "//*[@id='customers']/tbody/tr["; // changed customer to single quote
        String aferXpath_Company = "]/td[1]";  //Company is column 1

        String beforeXpath_Contact = "//*[@id='customers']/tbody/tr[";
        String aferXpath_Contact = "]/td[2]";  // Contact is column 2

        String beforeXpath_Country = "//*[@id='customers']/tbody/tr[";
        String aferXpath_Country = "]/td[3]";  // Country is column 3

        //Find number of rows so that we do not use hard coded values
        List<WebElement> totalRows = driver.findElements(By.xpath("//table[@id='customers']//tr"));
        int rows=totalRows.size();



        for (int i = 2; i <rows; i++) {  //we start from 2 because  1 is column name
            String actualXpath = beforeXpath_Company + i + aferXpath_Company;
            String companyName = driver.findElement(By.xpath(actualXpath)).getText();
            System.out.println(companyName);

            String actualXpath_Contact = beforeXpath_Contact + i + aferXpath_Contact;
            String contactName = driver.findElement(By.xpath(actualXpath_Contact)).getText();
            System.out.println(contactName);

            String actualXpath_Country = beforeXpath_Country + i + aferXpath_Country;
            String countryName = driver.findElement(By.xpath(actualXpath_Country)).getText();
            System.out.println(countryName);

           //Try to following to write to an Excel file in C drive
            Workbook wb = new HSSFWorkbook();
            CreationHelper createHelper = wb.getCreationHelper();
            Sheet sheet1 = wb.createSheet("Sheet1");

            Row row = sheet1.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue(createHelper.createRichTextString(companyName));

            FileOutputStream fileOut = new FileOutputStream("C:\\MyTemp\\Test.xls");
            wb.write(fileOut);
            fileOut.close();
        }
    }
}

Thanks in advance. 提前致谢。

Taking your existing table extraction code, you can do something like this: 利用现有的表提取代码,您可以执行以下操作:

public class WebTable1 {

  public static void main(String[] args) throws IOException {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.get("https://www.w3schools.com/html/html_tables.asp");

    String beforeXpath_Company = "//*[@id='customers']/tbody/tr["; // changed customer to single quote
    String aferXpath_Company = "]/td[1]";  //Company is column 1

    String beforeXpath_Contact = "//*[@id='customers']/tbody/tr[";
    String aferXpath_Contact = "]/td[2]";  // Contact is column 2

    String beforeXpath_Country = "//*[@id='customers']/tbody/tr[";
    String aferXpath_Country = "]/td[3]";  // Country is column 3

    //Find number of rows so that we do not use hard coded values
    List<WebElement> totalRows = driver.findElements(By.xpath("//table[@id='customers']//tr"));
    int rows=totalRows.size();


    // Create a workbook and a sheet in it
    Workbook wb = new HSSFWorkbook();
    Sheet sheet1 = wb.createSheet("Sheet1");

    // Create a table header
    Row row = sheet.createRow(0);
    row.createCell(0).setCellValue("Company name");
    row.createCell(1).setCellValue("Contact name");
    row.createCell(2).setCellValue("Country");


    for (int i = 2; i <rows; i++) {  //we start from 2 because  1 is column name
        String actualXpath = beforeXpath_Company + i + aferXpath_Company;
        String companyName = driver.findElement(By.xpath(actualXpath)).getText();

        String actualXpath_Contact = beforeXpath_Contact + i + aferXpath_Contact;
        String contactName = driver.findElement(By.xpath(actualXpath_Contact)).getText();

        String actualXpath_Country = beforeXpath_Country + i + aferXpath_Country;
        String countryName = driver.findElement(By.xpath(actualXpath_Country)).getText();

        Row row = sheet1.createRow(i - 1);
        row.createCell(0).setCellValue(companyName);
        row.createCell(1).setCellValue(contactName);
        row.createCell(2).setCellValue(countryName);
    }
    FileOutputStream fileOut = new FileOutputStream("C:\\MyTemp\\Test.xls");
    wb.write(fileOut);
    fileOut.close();
  }
}

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

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