不确定我缺少什么?获取数据无效

时间:2015-11-21 07:34:19

标签: java selenium-webdriver apache-poi

我正在尝试将网站上的数据写入excel,但在我的写入数据方法中,我得到10个空值而不是实际值。

代码在这里:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Retrieve {

  public static WebDriver driver;
  public static int rowcount=0;
  public static int cellcount=0;

  public static String T1,T2,T3,T4,T5,T6,T7,T8,T9,T10;
  public static String data[] = new String[10];

  public static void main(String[] args) throws IOException {
    openurl();
    write_data();
  }

  public static void openurl() {    

    System.setProperty("webdriver.chrome.driver", "E:\\Selenium-Webdriver\\Chrome_Driver\\chromedriver.exe");
    driver = new ChromeDriver();

    driver.get(URL);

    T1 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[1]/td[2]/a/b")).getText();
    T2 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[2]/td[2]/a/b")).getText();
    T3 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[3]/td[2]/a/b")).getText();
    T4 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[4]/td[2]/a/b")).getText();
    T5 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[5]/td[2]/a/b")).getText();
    T6 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[6]/td[2]/a/b")).getText();
    T7 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[7]/td[2]/a/b")).getText();
    T8 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[8]/td[2]/a/b")).getText();
    T9 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[9]/td[2]/a/b")).getText();
    T10 = driver.findElement(By.xpath("//*[@id='wpv-view-layout-34546-CPID79']/table/tbody/tr[10]/td[2]/a/b")).getText();   
  }

  public static void write_data() throws IOException {
    File file = new File("C:\\Users\\Documents\\Practice.xls"); 
    FileInputStream inputStream = new FileInputStream(file);

    Workbook MyWorkbook = null;  
    MyWorkbook = new HSSFWorkbook(inputStream);

    Sheet sheet = MyWorkbook.getSheet("sheet1");

    Row row = sheet.getRow(0);

    try {   
      int lendgth = data.length;
      for(int i = 0;i < data.length;i++) {   
        Row newrow = sheet.createRow(rowcount+1);
        Cell cell = newrow.createCell(cellcount);

        if(!data[i].equals("") || data[i].equals(null)) { 
          cell.setCellValue(data[i]);
          rowcount++;    
        }    
      }
    }catch(Exception E) {
      E.printStackTrace();      
    }   
    inputStream.close();    
    FileOutputStream outputStream = new FileOutputStream(file);

    MyWorkbook.write(outputStream);  
    outputStream.close();   
  }
}

我的所有数据均为空:if(!data[i].equals("") || data[i].equals(null))并获取nullpointerexception。

1 个答案:

答案 0 :(得分:2)

你有:

public static String data[] = new String[10];

但尚未为其指定任何值,所有元素都初始化为null

data包含:

[null, null, null, null, null, null, null, null, null, null]

您需要将T1, T2, ....的值分配给data[]

只是做:

data[0] = T1;
data[1] = T2;
....