我的代码。我在" userId"中输入我的登录名和密码。和#34;密码"领域。我想写一个条件,检查你何时点击"提交"按钮或可以登录系统。如果" userId"和#34;密码"是正确的,那么当你按下"提交"按钮加载新页面并发布代码的下一部分。但是我想在用户输入错误密码时添加一个条件。然后,按下"提交"按钮,消息"错误的登录信息"出现在页面上。任何人都可以帮我写这样的条件吗?
for(int i =0;i<userName.size();i++){
driver.findElement(By.id("userId")).sendKeys(userName.get(i));
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.id("password")).sendKeys(password.get(i));
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.id("Submit")).click();
//这里加载新页面
// change password
driver.findElement(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a")).click();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
// new password
String pwd = RandomStringUtils.random( 15, upper+smaller+number+character);
System.out.println("New password: " + pwd);
driver.findElement(By.id("newPassword")).sendKeys(pwd);
Thread.sleep(6000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
// confirm password
driver.findElement(By.id("confirmPassword")).sendKeys(pwd);
System.out.println("Confirm password: " + pwd);
Thread.sleep(10000);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
答案 0 :(得分:0)
请尝试以下操作:
点击“提交”按钮后;您可以检查网页上是否存在此元素:driver.findElement(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a"));
如果是这意味着在登录成功时加载了新页面,否则这是一个糟糕的登录。
点击提交按钮后,您可以尝试以下操作:
<强>更新强>
List<WebElement> myElemnt = driver.findElements(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a"));
loginSuccessful(myElemnt);
// loginSuccessful()函数的实现:
public boolean loginSuccessful(WebElement element){
try{
element;
return true;
}
catch(Exception e){
System.out.println("Bad Login Information!!");
return false;
}
}
<强> OLD 强>
String beforeTitle = driver.getTitle();
//put your Click Submit Button code here
String afterTitle = driver.getTitle();
if(beforeTitle.equals(afterTitle)){
System.out.println("Bad Login Information!!");
}