我正在尝试获取下拉列表值并插入到Excel文件中。
我已经编写了代码,但是页面上显示错误。
错误显示为“ java.lang.NullPointerException”。
下面是编写的代码:-
package test.neha;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.*;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Dropdown_extraction {
public static WebDriver driver;
WebDriverWait wait;
XSSFWorkbook workbook;
XSSFSheet sheet;
XSSFCell cell;
@Test
public void list_items() throws EncryptedDocumentException, InvalidFormatException, IOException
{
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("file:///C:/Users/neha.sharma/Desktop/File.html");
//driver.manage().window().maximize();
File src=new File("D:\\Drop_values.xlsx");
WebElement Element=driver.findElement(By.xpath("html/body/form/fieldset/p/select"));
Element.click();
FileInputStream fr=new FileInputStream(src);
workbook = (XSSFWorkbook) WorkbookFactory.create(fr);
XSSFSheet sh = workbook.getSheetAt(0);
List<WebElement> L= (List) driver.findElements(By.xpath("html/body/form/fieldset/p/select"));
for(WebElement e:L)
{
String value=e.getText();
System.out.println("Print the values as below:"+value);
System.out.println("Values are:"+ e.getText());
Row row=sh.getRow(0);
Cell[] cell=new Cell[10];
if(row==null)
{
for(int i=1;i<=10;i++)
{
cell[i]=row.createCell(i);
}
if(cell[1]==null)
{
cell[1].setCellType(Cell.CELL_TYPE_NUMERIC);
cell[1].setCellValue(value);
}
if(cell[2]==null)
{
cell[2].setCellType(Cell.CELL_TYPE_NUMERIC);
cell[2].setCellValue(value);
}
}
/*sh.getRow(1).createCell(0).setCellValue(value);
sh.getRow(2).createCell(1).setCellValue(value);
sh.getRow(3).createCell(2).setCellValue(value);
sh.getRow(4).createCell(3).setCellValue(value);
*/
FileOutputStream Output = new FileOutputStream(src);
workbook.write(Output);
Output.close();
}
/*Select element_list=new Select(driver.findElement(By.xpath("html/body/form/fieldset/p/select")));
element_list.selectByVisibleText("Apple");*/
}
}
任何人都可以让我知道此错误的解决方案。该错误主要显示在“创建单元格代码”中。 请帮帮我。