【问题标题】:Playwright - How to export the output data to Excel sheet (Java)Playwright - 如何将输出数据导出到 Excel 工作表 (Java)
【发布时间】:2022-11-15 01:13:05
【问题描述】:

我尝试将输出数据导出到 Excel 工作表,但所有值都存储为单个单元格。

public class Test{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        
                Playwright playwright=Playwright.create();
                //select which browser we need to launch and set headless mode
                Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
                
                
                Page page = browser.newPage();
                page.navigate("https://datatables.net/extensions/select/examples/initialisation/checkbox.html");
                
                for (int i = 1; i <= 10; i++) {
                
                Locator row=page.locator("table#example tr").nth(i); //represents entire row
                List<String>text=row.allInnerTexts();
                
                text.removeAll(Arrays.asList("", null));
                System.out.println(text.toString());
                }
                
    }

}




Output:
[   Airi Satou  Accountant  Tokyo   33  $162,700]
[   Angelica Ramos  Chief Executive Officer (CEO)   London  47  $1,200,000]
[   Ashton Cox  Junior Technical Author San Francisco   66  $86,000]
[   Bradley Greer   Software Engineer   London  41  $132,000]
[   Brenden Wagner  Software Engineer   San Francisco   28  $206,850]
[   Brielle Williamson  Integration Specialist  New York    61  $372,000]
[   Bruno Nash  Software Engineer   London  38  $163,500]
[   Caesar Vance    Pre-Sales Support   New York    21  $106,450]
[   Cara Stevens    Sales Assistant New York    46  $145,600]
[   Cedric Kelly    Senior Javascript Developer Edinburgh   22  $433,060]

我需要将值分配给每个列,而不是在单元格中存储完整值(Airi Satou Accountant Tokyo 33 $162,700)

【问题讨论】:

    标签: java arrays list arraylist playwright


    【解决方案1】:

    为行创建一个 for 循环,然后为列创建另一个循环

    for (int i = 1; i <= 10; i++) {
        System.out.println("Values for row " + i);
        for (int z = 0; z <= 6; z++) {        
            Locator cell=page.locator("(//tbody)[1]/tr[i]/td[z]"); 
            String text=cell.innerText();
            System.out.println(text.toString());
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多