使用 selenium 使用 apache poi 写入 excel 文件

请在您对此投反对票之前,我找不到读取网络表格并将其写入 Excel 文件的示例。如果您碰巧找到该链接,请提供它。我发现了很多关于如何写入 Excel 文件但不从 Web 表格部分读取的示例。


这是我的代码:


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);


        }

    }

}

提前致谢。


PIPIONE
浏览 138回答 1
1回答

泛舟湖上清波郎朗

使用现有的表提取代码,您可以执行以下操作:public class WebTable1 {&nbsp; public static void main(String[] args) throws IOException {&nbsp; &nbsp; System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe");&nbsp; &nbsp; WebDriver driver = new ChromeDriver();&nbsp; &nbsp; driver.get("https://www.w3schools.com/html/html_tables.asp");&nbsp; &nbsp; String beforeXpath_Company = "//*[@id='customers']/tbody/tr["; // changed customer to single quote&nbsp; &nbsp; String aferXpath_Company = "]/td[1]";&nbsp; //Company is column 1&nbsp; &nbsp; String beforeXpath_Contact = "//*[@id='customers']/tbody/tr[";&nbsp; &nbsp; String aferXpath_Contact = "]/td[2]";&nbsp; // Contact is column 2&nbsp; &nbsp; String beforeXpath_Country = "//*[@id='customers']/tbody/tr[";&nbsp; &nbsp; String aferXpath_Country = "]/td[3]";&nbsp; // Country is column 3&nbsp; &nbsp; //Find number of rows so that we do not use hard coded values&nbsp; &nbsp; List<WebElement> totalRows = driver.findElements(By.xpath("//table[@id='customers']//tr"));&nbsp; &nbsp; int rows=totalRows.size();&nbsp; &nbsp; // Create a workbook and a sheet in it&nbsp; &nbsp; Workbook wb = new HSSFWorkbook();&nbsp; &nbsp; Sheet sheet1 = wb.createSheet("Sheet1");&nbsp; &nbsp; // Create a table header&nbsp; &nbsp; Row row = sheet.createRow(0);&nbsp; &nbsp; row.createCell(0).setCellValue("Company name");&nbsp; &nbsp; row.createCell(1).setCellValue("Contact name");&nbsp; &nbsp; row.createCell(2).setCellValue("Country");&nbsp; &nbsp; for (int i = 2; i <rows; i++) {&nbsp; //we start from 2 because&nbsp; 1 is column name&nbsp; &nbsp; &nbsp; &nbsp; String actualXpath = beforeXpath_Company + i + aferXpath_Company;&nbsp; &nbsp; &nbsp; &nbsp; String companyName = driver.findElement(By.xpath(actualXpath)).getText();&nbsp; &nbsp; &nbsp; &nbsp; String actualXpath_Contact = beforeXpath_Contact + i + aferXpath_Contact;&nbsp; &nbsp; &nbsp; &nbsp; String contactName = driver.findElement(By.xpath(actualXpath_Contact)).getText();&nbsp; &nbsp; &nbsp; &nbsp; String actualXpath_Country = beforeXpath_Country + i + aferXpath_Country;&nbsp; &nbsp; &nbsp; &nbsp; String countryName = driver.findElement(By.xpath(actualXpath_Country)).getText();&nbsp; &nbsp; &nbsp; &nbsp; Row row = sheet1.createRow(i - 1);&nbsp; &nbsp; &nbsp; &nbsp; row.createCell(0).setCellValue(companyName);&nbsp; &nbsp; &nbsp; &nbsp; row.createCell(1).setCellValue(contactName);&nbsp; &nbsp; &nbsp; &nbsp; row.createCell(2).setCellValue(countryName);&nbsp; &nbsp; }&nbsp; &nbsp; FileOutputStream fileOut = new FileOutputStream("C:\\MyTemp\\Test.xls");&nbsp; &nbsp; wb.write(fileOut);&nbsp; &nbsp; fileOut.close();&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java