Selenium Webdriver | Исключение при чтении имени пользователя и пароля для Gmail из Excelsheet - PullRequest
0 голосов
/ 11 сентября 2018

Ниже приведено исключение, которое я получаю при попытке прочитать имя пользователя и пароль gmail из файла Excel, хранящегося в определенном месте, также пытался добавить 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);
    }
}

1 Ответ

0 голосов
/ 11 сентября 2018

Необходимо указать полный путь к файлу в качестве параметра для

new FileInputStream(path)

конструктор, но вы указываете только путь к папке (public static final String Path_TestData = "D: \ selenuim \";)

Так измените строку

FileInputStream ExcelFile = new FileInputStream(path);

до

FileInputStream ExcelFile = new FileInputStream(path+SheetName);

в методе setExcelFile

...