Java 不知道我错过了什么?获取空数据
我试图将数据从网站写入excel,但在我的write data方法中,我得到的是10个空值,而不是实际值 代码如下:Java 不知道我错过了什么?获取空数据,java,selenium-webdriver,apache-poi,Java,Selenium Webdriver,Apache Poi,我试图将数据从网站写入excel,但在我的write data方法中,我得到的是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.u
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();
}
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.chrome.ChromeDriver;
公共类检索{
公共静态WebDriver;
公共静态int rowcount=0;
公共静态int cellcount=0;
公共静态字符串T1、T2、T3、T4、T5、T6、T7、T8、T9、T10;
公共静态字符串数据[]=新字符串[10];
公共静态void main(字符串[]args)引发IOException{
openurl();
写入数据();
}
公共静态void openurl(){
System.setProperty(“webdriver.chrome.driver”,“E:\\Selenium webdriver\\chrome\u driver\\chromedriver.exe”);
驱动程序=新的ChromeDriver();
获取(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()引发IOException{
File File=新文件(“C:\\Users\\Documents\\Practice.xls”);
FileInputStream inputStream=新的FileInputStream(文件);
工作簿MyWorkbook=null;
My工作簿=新的HSSF工作簿(inputStream);
Sheet Sheet=MyWorkbook.getSheet(“sheet1”);
Row Row=sheet.getRow(0);
试试{
int lendgth=data.length;
对于(inti=0;i
如果(!data[I].equals(“”| | data[I].equals(null))并获取nullpointerexception,则在:获取所有数据null。
您有:
public static String data[] = new String[10];
但是没有给它赋值,所有元素都初始化为null
数据
包含:
[null, null, null, null, null, null, null, null, null, null]
您需要将T1,T2,….
的值分配给data[]
只要做:
data[0] = T1;
data[1] = T2;
....
好。。。您从不初始化
数据
元素,因此它们都是null
。也许你不想做T1=。。。;T2=代码>但您希望执行数据[0]=。。。;数据[1]=代码>?哦,是的。。你说得对。。非常感谢..是的,伙计,这是我愚蠢的错误,但不知道为什么我忘了初始化。感谢您的快速帮助:)