【发布时间】:2021-02-21 23:22:57
【问题描述】:
我正在尝试从 Internet 获取一些数据,然后将其保存在一个 txt 文件中,但我得到一个 Nullpointer 异常,内容如下: 信息:检测到的方言:W3C mypackage.testClass.main(testClass.java:31) 处的线程“main”java.lang.NullPointerException 中的异常。 所以,它实际上将我引向这一行:byte[] contentInBytes = aaa.getBytes();
代码如下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.*;
public class testClass {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.blablawebsite.com/path");
List<WebElement> urls = driver.findElements(By.xpath("//li[contains(@text(),'text')]"));
int i;
try {
File file = new File("/path/to/file/text.txt");
FileOutputStream fos = new FileOutputStream(file);
if(!file.exists())
file.createNewFile();
for(i=0; i <urls.size(); i++) {
String aaa = urls.get(i).getAttribute("data-asin");
byte[] contentInBytes = aaa.getBytes();
fos.write(contentInBytes);
fos.flush();
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
所以我猜
String aaa = urls.get(i).getAttribute("data-asin");这个属性是不存在的。
标签: java selenium nullpointerexception fileoutputstream