gpt4 book ai didi

Java/Selenium WebDriver : Focus don't skip to ELSE block if a webelement is not found on page

转载 作者:行者123 更新时间:2023-11-30 06:19:53 25 4
gpt4 key购买 nike

我正在使用 Java 开发 Selenium WebDriver,我需要检查输入无效用户名和密码时的场景,应用程序应该抛出警告消息。如果输入正确的凭据,则不会显示警告消息。我在下面编写了一段代码来验证有关错误登录凭据的警告消息。但是,当我输入有效的详细信息时,我的代码不会跳到 else block ,而是失败说它无法识别元素 ("//*[@class=' Small-9 Small-pull-1 列内容']").

代码:

try {           
if (new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "No Password Found for"))){

String text = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");
System.out.println(text);

if(text.contentEquals("No Password Found for")){
driver.navigate().refresh();

Assert.fail("Unable to login to application");
}
}

else if(new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it."))){

String retext = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");

System.out.println(retext);

if(retext.contentEquals("Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it.")) {
driver.navigate().refresh();

Assert.fail("Unable to login to application");
}


}

else if (new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "Time Out"))){
String timeouttext = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");

if(timeouttext.contentEquals("You have specified an invalid User Name or Password. Please check and try again")){
driver.navigate().refresh();

Assert.fail("Unable to login to application"); }


}

带有失败消息的 HTML block :

<div class="column">
<div class="form-field ">
<div class="popup-login-error visible -full error" id="j_idt65">
<div class="row"><span class="icon-cancel-thin color-white" messagepopupclose=""></span>
<div class="small-2 column icon"><i class="icon-exclamation-circle"></i></div>
<div class="small-9 small-pull-1 column content">You have specified an invalid User Name or Password. Please check and try again</div>
<div class="small-9 small-pull-1 column content">No Password Found for </div>
</div>
</div>
</div>
</div>

最佳答案

我已经优化了您的代码块,如下所示:

  • 在任何情况下都必须将 driver.findElement(By.name("subscribed")).click();try{} block 中取出 click()它。
  • 引发WebDriverWait,使文本无效的用户名或密码出现在HTML DOM
  • 保持错误文本用户名或密码无效xpath不变。
  • getText() 替换为 getAttribute("innerHTML")
  • 保持 contentEquals()Assert.fail() 逻辑不变。
  • 删除了 driver.navigate().refresh();,因为它可能会引发 StaleElementReferenceException
  • 如果您的尝试{}失败错误消息,您的程序将打印显示仪表板
  • 这是您的工作代码块:

    //code block
    driver.findElement(By.name("submitted")).click();
    try {
    new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "invalid User Name or Password"));
    String text = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");
    System.out.println(text);
    if (text.contentEquals("You have specified an invalid User Name or Password. Please check and try again") || text.contentEquals("Time Out") || text.contentEquals("Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it."))
    {
    Assert.fail("Unable to login to application");
    }
    } catch (Exception e) {
    System.out.println("DAashboard is displayed");
    // rest of code block
    }

关于Java/Selenium WebDriver : Focus don't skip to ELSE block if a webelement is not found on page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48396714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com