繁体   English   中英

没有在excel表中写

[英]didnot write in excel sheet

我想在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());

问题似乎与您的for循环结构有关。 一些注意事项,首先是关于一些观察。 您需要在此处进行高隐式等待,因为该网站加载结果的速度很慢。 您的行xpath什么都没找到,但是你的列xpath工作,即使它移植到Protactor时没有。 我正在使用ID“f1”的定位器绘制替代方法并拆分生成的文本,但事实证明这相当于您的xpath“column”找到的内容。 这里的关键是知道新行开始的时间以及项目不再是您想要的数据。 当索引是3的倍数时,行开始,因为每行有3个字段(名称和2个数字)。 我们不关心的东西从数字1开始。我没有编写代码将数据放入Excel中,但我指出了你想要的地方。 这是我的代码(由于您的Chrome驱动程序或其中任何一个位置,以及因为您有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) {
    System.setProperty("webdriver.chrome.driver", "bin/chromedriver");
    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get(
            "https://careernavigator.naukri.com/sales-executive-retail-careers-in-mahindra-and-mahindra-financial-services-15731");
    /// the row xpath that ws once here found nothing
    List<WebElement> column = driver.findElements(
            By.xpath("(//*[name()='svg'])[2]//*[name()='text']//*[name()='tspan' and (@dy=4 or @dy='3.5')]"));
    int spreadSheetRowNum = 0;
    int spreadSheetColumnNum = 0;
    WebElement f1 = driver.findElement(By.id("f1"));
    for (int i = 0; i < column.size(); i++) {
        if (column.get(i).getText().equalsIgnoreCase("1")) {
            // reached end of meaningful fields
            break;
        }
        if (i % 3 == 0) {// start of new row
            spreadSheetRowNum++;
            System.out.println("here create row: " + spreadSheetRowNum);
            spreadSheetColumnNum = 1;// assuming excel column A is column 1
        } else {
            spreadSheetColumnNum++;
        }

        System.out.println("for column list " + i + " text is:");
        System.out.println(column.get(i).getText());
        System.out.println("write it to row " + spreadSheetRowNum + " column " + spreadSheetColumnNum);
    }

    String[] f1Arr = f1.getText().split("\n");
    System.out.println("if you prefer to use the f1 array, its contents are:");
    for (int i = 0; i < f1Arr.length; i++) {
        System.out.println("f1[" + i + "] = " + f1Arr[i]);
    }
    driver.close();
}
}

这是我得到的输出,表明在各个步骤中你需要做什么以及你需要做什么:

here create row: 1
for column list 0 text is:
Mahindra and Mahindra Financia..
write it to row 1 column 1
for column list 1 text is:
4.8
write it to row 1 column 2
for column list 2 text is:
2.4
write it to row 1 column 3
here create row: 2
for column list 3 text is:
Tata Motors
write it to row 2 column 1
for column list 4 text is:
5.0
write it to row 2 column 2
for column list 5 text is:
2.6
write it to row 2 column 3
here create row: 3
for column list 6 text is:
Mahindra and Mahindra
write it to row 3 column 1
for column list 7 text is:
4.6
write it to row 3 column 2
for column list 8 text is:
2.9
write it to row 3 column 3
if you prefer to use the f1 array, its contents are:
f1[0] = Mahindra and Mahindra Financia..
f1[1] = 4.8
f1[2] = 2.4
f1[3] = Tata Motors
f1[4] = 5.0
f1[5] = 2.6
f1[6] = Mahindra and Mahindra
f1[7] = 4.6
f1[8] = 2.9
f1[9] = 1
f1[10] = 1
f1[11] = 2
f1[12] = 2
f1[13] = 3
f1[14] = 3
f1[15] = 4
f1[16] = 4
f1[17] = 5
f1[18] = 5
f1[19] = 6
f1[20] = 6
f1[21] = 7
f1[22] = 7
f1[23] = 8
f1[24] = 8
f1[25] = Avg.Exp
f1[26] = Avg.Sal
f1[27] = In lacs
f1[28] = Avg.Exp
f1[29] = Avg.Sal
f1[30] = In lacs
f1[31] = Top Companies
f1[32] = Top Companies
f1[33] = Top Companies
f1[34] = Top Companies
f1[35] = 1.6
f1[36] = 4.9
f1[37] = 2.4
f1[38] = 1.6
f1[39] = 7.0
f1[40] = 2.6
f1[41] = 1.3
f1[42] = 7.2
f1[43] = 2.9
f1[44] = View 22 more Companies.

暂无
暂无

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

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