以下是我尝试从存储在特定位置的Excel文件中读取gmail用户名和密码时遇到的异常,并尝试在项目中添加excel。请帮助解决此问题。
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.io.FileNotFoundException: D:\selenuim (Access is denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at SeleniumPackage.ExcelUtils.setExcelFile(ExcelUtils.java:45)
at SeleniumPackage.ExcelUtils.main(ExcelUtils.java:36)
这是读取用户名和密码以登录gmail的代码。
public class ExcelUtils {
public static final String Path_TestData = "D:\\selenuim\\";
public static final String File_TestData = "TestData.xls";
public static HSSFSheet ExcelWSheet;
public static HSSFWorkbook ExcelWBook;
public static HSSFCell Cell;
public static HSSFRow Row;
public static WebDriver driver;
public static void main(String[] args) throws Exception {
File pathToBinary = new File("C:\\Users\\sajjankumar.parjapat\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(ffBinary,profile);
driver.manage().window().maximize();
setExcelFile(Path_TestData,File_TestData);
Login();
}
public static void setExcelFile(String path, String SheetName) throws Exception {
try {
FileInputStream ExcelFile = new FileInputStream(path);
ExcelWBook = new HSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
}
catch (Exception e) {
throw (e); }
}
public static String getCellData(int RowNum, int ColNum) {
try {
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
String CellData = Cell.getStringCellValue();
System.out.println(CellData);
return CellData ;
}
catch (Exception e) {
throw (e); }
}
public static void Login() throws InterruptedException {
String url = "https://www.google.com";
driver.get(url);
driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).click();
Thread.sleep(2000);
String Username = getCellData(1,1) ;
String pwd = getCellData(1,2);
driver.findElement(By.id("Email")).sendKeys(Username);
Thread.sleep(2000);
driver.findElement(By.id("next")).click();
Thread.sleep(2000);
driver.findElement(By.id("Passwd")).sendKeys(pwd);
Thread.sleep(2000);
driver.findElement(By.id("signIn")).click();
Thread.sleep(2000);
try {
Boolean oldVersion = driver.findElement(By.xpath(".//*[@id='browsers']/h2")).isDisplayed();
if(oldVersion) {
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='browsers']/p/b[2]/a")).click();
}
}
catch (Exception e) {
throw e; }
Thread.sleep(5000);
WebElement signOut = driver.findElement(By.xpath(".//*[@id='gb_71']"));
signOut.click();
Thread.sleep(2000);
}
}
答案 0 :(得分:0)
您需要提供文件的完整路径作为
的参数new FileInputStream(path)
构造函数,但是您只给出文件夹路径(公共静态最终String Path_TestData =“ D:\ selenuim \”;)
因此更改行
FileInputStream ExcelFile = new FileInputStream(path);
到
FileInputStream ExcelFile = new FileInputStream(path+SheetName);
在setExcelFile方法中