硒网络驱动程序 |从 excelsheet 读取 gmail 的用户名和密码时出现异常

Selenium Webdriver | Exception while read username and password for gmail from excelsheet

以下是我在尝试从存储在特定位置的 Excel 文件中读取 gmail 的用户名和密码时遇到的异常,并尝试在项目中添加 excel。请帮助解决这个问题。

Exception

  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);
    }
}

您需要将文件的完整路径作为参数提供给

new FileInputStream(path)

构造函数,但您只提供文件夹路径 (public static final String Path_TestData = "D:\selenuim\";)

所以换行

FileInputStream ExcelFile = new FileInputStream(path);

FileInputStream ExcelFile = new FileInputStream(path+SheetName);

在setExcelFile方法中