簡體   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