0

所以,这里有两个 div

<div class="th_pr"><input id="user_email" class="accounts_input" type="text" size="30" placeholder="Email" name="user[email]"></input><p class="accounts_error_text" style="display: block;">
  email is invalid
</p></div>
<div class="th_pr"><input id="user_password" class="accounts_input" type="password" size="30" placeholder="Password" name="user[password]" autocomplete="off"></input><p class="accounts_error_text" style="display: block;">
  password can't be blank
</p></div>

我需要通过文本获取带有文本“电子邮件无效”和“密码不能为空”的那些元素,因为它会因输入而异。

我一直在尝试使用 xpath 完成此操作:

By.xpath("//p[contains(.,'email is invalid')]")

By.xpath("//p[contains(.,'password be blank')]")

但我什么也没得到。

resultEmail = ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[contains(.,'email is invalid')]")).apply(driver);

返回 true,尽管元素是可见的。

请帮忙。

4

3 回答 3

1

试试 xpath

//input[@id='user_email']/following-sibling::p
//input[@id='user_password']/following-sibling::p

然后你有

WebElement emailParagraph = driver.findElement(By.xpath("//input[@id='user_email']/following-sibling::p"));
System.out.println(emailParagraph.getText());
于 2013-08-07T07:28:09.183 回答
0

您是否尝试text()在 xpath 中使用该方法?

driver.findElement(By.xpath("//p[contains(text(), 'email is invalid')]"));

而不是使用.?

于 2013-08-07T07:54:56.050 回答
0

请试试这个:

WebElement userEmailErrorMessage = driver.findElement(By.cssSelector("div[class=\"th_pr\"]:nth-child(1) > p"));

WebElement userPasswordErrorMessage = driver.findElement(By.cssSelector("div[class=\"th_pr\"]:nth-child(2) > p"));


使用这些元素,您将能够阅读相应输入控件的错误消息。

于 2013-08-07T07:55:04.993 回答