selenium - Selenium 网络驱动程序 | 从excelsheet读取gmail的用户名和密码时出现异常
问题描述
下面是我在尝试从存储在特定位置的 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);
}
}
解决方案
您需要将文件的完整路径作为参数提供给
new FileInputStream(path)
构造函数,但您只提供文件夹路径(public static final String Path_TestData = "D:\selenuim\";)
所以换行
FileInputStream ExcelFile = new FileInputStream(path);
至
FileInputStream ExcelFile = new FileInputStream(path+SheetName);
在方法 setExcelFile
推荐阅读
- python - 如何在数据集中制作包含三个变量的等高线图?
- python - 如何使用 end = "" 命令从 for 循环中添加字符串,Python 3
- javascript - .Onclick 和 .addEventListener("click") 都不限于单击按钮的时间
- python - 是否可以使用 scapy 接收不同进程/线程的 ping 答案
- sql - 在 psql 更新查询中缺少表的 FROM 子句条目
- razor - CSHTML Razor @Html.NavItem 内联 CSS 样式
- c++ - 两种不同类型的相同模板类/结构的运算符重载?
- python - 如何使用 aiofiles 异步腌制大量文件?
- c - 计算c中两个时间字符串的差异时间?
- angular - 获取活动模块:Angular 7