Xpath返回false

时间:2016-05-24 09:30:24

标签: java xpath selenium-webdriver

系统返回验证错误:

抱歉,test @ y.com不会被识别为用户名或电子邮件地址。

Html错误代码消息:

<div class="error validate">
<h2 class="element-invisible">Error message</h2>
Sorry, <em class="placeholder">test@y.com</em> is not recognized as a user name or an e-mail address.</div>

我正在尝试Xpath:

@FindBy(xpath="//div[\n" +
            "    @class='errorvalidate' \n" +
            "    and contains(., 'Sorry,')\n" +
            "    and contains(., 'is not recognized as a user name or an e-mail address.')\n" +
            "]")

我正在尝试动态地查找系统生成的验证消息以进行断言,但是上面的xpath无法找到消息元素。

2 个答案:

答案 0 :(得分:1)

我已经重新格式化了你的xpath,而且类名在这里也有意义,

@FindBy(xpath="//div[@class='error validate' and contains(., 'Sorry,') and contains(., 'is not recognized as a user name or an e-mail address.')]")

答案 1 :(得分:-1)

嗨,请尝试如下

// now before getting error message 
// verify that class error validate exits 
// then only get error message 

List<WebElement> doesErrorClassExists = driver.findElements(By.xpath("//*[@class='error validate']"));
WebElement myErrorClass = driver.findElement(By.xpath("//*[@class='error validate']"));

if(doesErrorClassExists.size() > 0){
System.out.println("Error message is : " + myErrorClass.getText());
}