无法使用xpath定位嵌套文本

iih3973s  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(286)

我是新的自动化,并试图测试一个不成功的登录。输入错误的密码时,网站上会显示一个错误弹出窗口。我正在尝试编写一个测试来确认显示的错误文本。
代码试验:

@Test
    public void IncorrectPasswordMessage() {

        boolean b=driver.findElement(By.xpath("//*[@id=\"errors\"]/h2")).isDisplayed();
        Assert.assertTrue(b);

    }

下面是html。如何更改脚本以确认网页上显示的嵌套文本“存在以下错误”?

<form id="login_form" action="/cs/form/customer-login" method="AJAX" class="cforms pad-top1 span6">
    <!--|cid=1400679170853|type=Forms_P|-->
    <div id="errors" style="">
        <h2>There are the following errors.</h2>
        <ul><li>The number and password you have entered do not match. Please enter it again.</li></ul>
    </div>
    <input type="hidden" name="_form_url" value="">
    <input type="hidden" name="_success_url" value="">
    <input type="hidden" name="_failure_url" value="">
</form>
cigdeys3

cigdeys31#

元素是ajax元素,所以 click() 在元素上,您需要为 visibilityOfElementLocated() 您可以使用以下任一定位器策略:
CSS选择器:

@Test
    public void IncorrectPasswordMessage() {
        WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#errors>h2")));
        // lines of code
        Assert.assertTrue(b);
    }

xpath:

@Test
    public void IncorrectPasswordMessage() {
        WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='errors']/h2")));
        // lines of code
        Assert.assertTrue(b);
    }

相关问题