ERJAN
ERJAN

Reputation: 24500

How to get text out of div class tag in selenium webdriver?

<div class="error-text">invalid login or password.</div>

HOw do I retrieve the actual text "invalid login or password"? I m running the test on testNG. Here is my test java code:

WebElement wrong_message = firefox.findElement(By.xpath("//*[@id=\"login-form\"]/div[3]"));
      System.out.println("message is here:" + wrong_message.getText().toString());
      Assert.assertNotNull(wrong_message);

But the output of testrun has empty string instead of "invalid login or password".

Upvotes: 1

Views: 956

Answers (1)

aberna
aberna

Reputation: 5814

Html code for your sample is missing so by what I understand of the structure of the page I would suggest:

WebElement wrong_message =
firefox.findElement(By.xpath("//div[text()[contains(.,'invalid')]]"));

This way it will extract the text from the div only if it contains the string "invalid"

Upvotes: 3

Related Questions