-1
我想從網站寫入數據到excel,但在我的寫入數據方法中,我得到了10個空值而不是實際值。不知道我錯過了什麼?獲取數據null
代碼是在這裏:
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異常。
嗯......你永遠不會初始化'data'元素,所以他們都是'空'。也許你不想做'T1 = ...; T2 = ...;'而是你想做'data [0] = ...; data [1] = ...;'? – Seelenvirtuose
Ohhh,你..你是對的人..非常感謝.. –