Java / Selenium WebDriver:如果在页面上找不到webelement,焦点不会跳到ELSE块

时间:2018-01-23 07:56:22

标签: java html selenium selenium-webdriver selenium-chromedriver

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

代码:

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阻止:

<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>

3 个答案:

答案 0 :(得分:1)

我已按如下方式优化您的代码块:

  • 在任何情况下,driver.findElement(By.name("submitted")).click();阻止try{} click()
  • WebDriverWait text 无效的用户名或密码 HTML DOM 强>
  • 保持xpath错误文字无效的用户名或密码不变。
  • getAttribute(“innerHTML”)取代getText()
  • 保持您的contentEquals()Assert.fail()逻辑不受影响。
  • 删除driver.navigate().refresh();,因为它可能会邀请 StaleElementReferenceException
  • 包含您的try{} 失败以查看程序将要打印的错误消息显示信息中心
  • 以下是您的工作代码块:

    //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
    }
    

答案 1 :(得分:0)

  

但是,当我输入有效的详细信息时,我的代码不会跳到else块,而是无法说它无法识别元素(“// * [@ class ='small-9 small-pull- 1列内容']“)。

当然不是因为isDisplayed();方法只能在现有的WEB元素上调用!

你的逻辑有一个缺陷,但可以很快修复:)

而不是使用if-else使用try-catch

try {
    driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")); 
    //you don't need a bolean. You just need to check whether element exists or not


    //here should be code when element DOES exist - error is visible
} catch (NoSuchElementException e) {
    //here should be code when element DOES NOT exist - error not visible
}

答案 2 :(得分:0)

请注意,有两个元素可以满足您要查找的属性:

By.xpath("//*[@class='small-9 small-pull-1 column content']")

// The two elements are:
<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>

您可以使用findElements

处理它们的列表
List<WebElement> list = driver.findElements(By.xpath("//*[@class='small-9 small-pull-1 column content']"))

或者只是专门打电话给每个人(记住xpath's 1-based indexing):

By.xpath("(//*[@class='small-9 small-pull-1 column content'])[1]")
By.xpath("(//*[@class='small-9 small-pull-1 column content'])[2]")


作为旁注:它被认为是catch(Exception e)的坏习惯,因为它使您无法确定代码的错误。尝试指定可能抛出的确切异常类型,或者至少打印StackTrace(在catch中使用e.printStackTrace())以进行进一步调试。