我正在尝试使用所有有效和无效的输入验证登录部分 .
这是我试过的代码:
public void login_Valid_Invalid_Combinations() throws BiffException, IOException, InterruptedException
{
String FilePath = "D://credentials.xls";
FileInputStream FIS = new FileInputStream(FilePath);
Workbook WB = Workbook.getWorkbook(FIS);
Sheet SH = WB.getSheet(0);
for(int row =0; row<= SH.getRows()-1; row++)
{
String userNAME = SH.getCell(0, row).getContents();
String passWORD = SH.getCell(1, row).getContents();
System.out.println("USERMANE : "+userNAME + " PASSWORD : "+passWORD);
driver.get("LOGIN PAGE URL");
driver.findElement(By.id("Email")).sendKeys(userNAME);
driver.findElement(By.id("Password")).sendKeys(passWORD);
driver.findElement(By.id("btnlogin")).click();
System.out.println(driver.findElement(By.xpath("//*[@id='eError']")).getText());
Thread.sleep(2000);
String URL = driver.getCurrentUrl();
System.out.println(URL);
if (URL.equals("URL AFTER SUCCESFULL LOGIN"))
{
System.out.println("Login Successfull");
}
else
{
System.out.println("Login Failed");
}
}
driver.close();
}
我想在每次登录失败时显示错误消息,这很正常 . 但当 login is successful 具有有效输入时,它显示:
“org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{”method“:”xpath“,”selector“:”// * [@ id ='eError']“}”
因为错误消息未成功登录 .
你能帮我解决一下这个问题 .
3 回答
对于这种情况,我不是try / catch的粉丝 . 您想要检查页面上是否存在元素,是否打印出包含的文本,否则成功登录,
你可以更换线路
有了这个:
try
块中的上述代码查找带有xpath的元素 . 如果它存在,则继续下一行以打印文本 . 如果元素不存在,则抛出异常并将其捕获到catch
块中并安全继续 .如果成功,您将不会收到错误消息,在这种情况下,行下面将抛出NoSuchElement Exception
我们需要将上面的代码放在try块中,然后它将处理异常 . 下面是更新的代码 .
如果它对您有帮助,请告诉我 .