没有写在excel表格中

我想在 excel 中写入我的数据。但它没有以格式化的方式写入


driver.get("https://careernavigator.naukri.com/sales-executive-retail-careers-in-mahindra-and-mahindra-financial-services-15731");

List<WebElement> row = driver.findElements(By.xpath("(//*[name()='svg'])[2]//*[name()='rect' and @height='40']"));

        List<WebElement> column = driver.findElements(By.xpath("(//*[name()='svg'])[2]//*[name()='text']//*[name()='tspan' and (@dy=4 or @dy='3.5')]"));

         for (int i=0;i<column.size();i++) {

            System.out.println(column.get(i).getText());

            XSSFRow row1 = sheet.createRow(i);

            for(int j=0;j<4;j++) 

            {

                Cell cell1 = row1.createCell(j);    

                cell1.setCellValue(column.get(j).getText());


狐的传说
浏览 91回答 1
1回答

明月笑刀无情

问题似乎与您的 for 循环结构有关。首先,关于一些观察的几个注意事项。您需要在这里进行高隐性等待,因为该站点加载结果的速度很慢。你的行 xpath 没有找到任何东西,但你的列 xpath 有效,即使当我移植到 Protactor 时它没有。我正在草拟一个替代方案,使用 id "f1" 的定位器并拆分结果文本,但事实证明这等同于你的 xpath "column" 找到的内容。这里的关键是要知道新行何时开始以及这些项目何时不再是您真正想要的数据。当索引是 3 的倍数时,行开始,因为每行有 3 个字段(一个名称和 2 个数字)。我们不关心的内容以数字 1 开头。我没有编写代码将数据放入 Excel 中,但我指出了您要放置的位置。import java.util.List;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class ShonaDriver {public static void main(String[] args) {&nbsp; &nbsp; System.setProperty("webdriver.chrome.driver", "bin/chromedriver");&nbsp; &nbsp; WebDriver driver = new ChromeDriver();&nbsp; &nbsp; driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);&nbsp; &nbsp; driver.manage().window().maximize();&nbsp; &nbsp; driver.get(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://careernavigator.naukri.com/sales-executive-retail-careers-in-mahindra-and-mahindra-financial-services-15731");&nbsp; &nbsp; /// the row xpath that ws once here found nothing&nbsp; &nbsp; List<WebElement> column = driver.findElements(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; By.xpath("(//*[name()='svg'])[2]//*[name()='text']//*[name()='tspan' and (@dy=4 or @dy='3.5')]"));&nbsp; &nbsp; int spreadSheetRowNum = 0;&nbsp; &nbsp; int spreadSheetColumnNum = 0;&nbsp; &nbsp; WebElement f1 = driver.findElement(By.id("f1"));&nbsp; &nbsp; for (int i = 0; i < column.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (column.get(i).getText().equalsIgnoreCase("1")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // reached end of meaningful fields&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (i % 3 == 0) {// start of new row&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spreadSheetRowNum++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("here create row: " + spreadSheetRowNum);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spreadSheetColumnNum = 1;// assuming excel column A is column 1&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spreadSheetColumnNum++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("for column list " + i + " text is:");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(column.get(i).getText());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("write it to row " + spreadSheetRowNum + " column " + spreadSheetColumnNum);&nbsp; &nbsp; }&nbsp; &nbsp; String[] f1Arr = f1.getText().split("\n");&nbsp; &nbsp; System.out.println("if you prefer to use the f1 array, its contents are:");&nbsp; &nbsp; for (int i = 0; i < f1Arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("f1[" + i + "] = " + f1Arr[i]);&nbsp; &nbsp; }&nbsp; &nbsp; driver.close();}}这是我得到的输出,指示那里有什么以及您需要在各个步骤中做什么:here create row: 1for column list 0 text is:Mahindra and Mahindra Financia..write it to row 1 column 1for column list 1 text is:4.8write it to row 1 column 2for column list 2 text is:2.4write it to row 1 column 3here create row: 2for column list 3 text is:Tata Motorswrite it to row 2 column 1for column list 4 text is:5.0write it to row 2 column 2for column list 5 text is:2.6write it to row 2 column 3here create row: 3for column list 6 text is:Mahindra and Mahindrawrite it to row 3 column 1for column list 7 text is:4.6write it to row 3 column 2for column list 8 text is:2.9write it to row 3 column 3if you prefer to use the f1 array, its contents are:f1[0] = Mahindra and Mahindra Financia..f1[1] = 4.8f1[2] = 2.4f1[3] = Tata Motorsf1[4] = 5.0f1[5] = 2.6f1[6] = Mahindra and Mahindraf1[7] = 4.6f1[8] = 2.9f1[9] = 1f1[10] = 1f1[11] = 2f1[12] = 2f1[13] = 3f1[14] = 3f1[15] = 4f1[16] = 4f1[17] = 5f1[18] = 5f1[19] = 6f1[20] = 6f1[21] = 7f1[22] = 7f1[23] = 8f1[24] = 8f1[25] = Avg.Expf1[26] = Avg.Salf1[27] = In lacsf1[28] = Avg.Expf1[29] = Avg.Salf1[30] = In lacsf1[31] = Top Companiesf1[32] = Top Companiesf1[33] = Top Companiesf1[34] = Top Companiesf1[35] = 1.6f1[36] = 4.9f1[37] = 2.4f1[38] = 1.6f1[39] = 7.0f1[40] = 2.6f1[41] = 1.3f1[42] = 7.2f1[43] = 2.9f1[44] = View 22 more Companies.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java