user2637419
user2637419

Reputation: 25

Can't find element using xpath

So, here are two divs

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

I need to get those elements with texts "email is invalid" and "password can't be blank" by text, cause it will differ depending on input.

I've been trying to complete this using xpath :

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

and

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

but i get nothing.

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

returns true, although the element is visible.

Please help.

Upvotes: 0

Views: 7753

Answers (3)

Yi Zeng
Yi Zeng

Reputation: 32855

Try xpath

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

Then you have

WebElement emailParagraph = driver.findElement(By.xpath("//input[@id='user_email']/following-sibling::p"));
System.out.println(emailParagraph.getText());

Upvotes: 1

Abhijeet Vaikar
Abhijeet Vaikar

Reputation: 1656

Please try this:

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"));


Using these elements you will be able to read the error messages for the respective input controls.

Upvotes: 0

Mark Rowlands
Mark Rowlands

Reputation: 5453

Did you try using the text() method within the xpath?

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

Rather than using the .?

Upvotes: 0

Related Questions