关于html:Java / Selenium WebDriver:如果在页面上未找到Webelement,请不要将焦点移至ELSE块

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

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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 a€?Forgot Passworda€? 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 a€?Forgot Passworda€? 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阻止:

1
2
3
4
            <span class="icon-cancel-thin color-white" messagepopupclose=""></span>
                <i class="icon-exclamation-circle">
                You have specified an invalid User Name or Password. Please check and try again
                No Password Found for

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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    //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 a€?Forgot Passworda€? 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
2
3
4
5
By.xpath("//*[@class='small-9 small-pull-1 column content']")

// The two elements are:
You have specified an invalid User Name or Password. Please check and try again
No Password Found for

您可以使用findElements

处理它们的列表

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

或者只是专门调用它们中的每一个(注意xpath基于1的索引):

1
2
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())以进行进一步的调试。