javaselenium-webdriverdom

Selenium XPath returns different text than chrome developer console


I have the following tag:

<div role="alert" class="fade alert alert-danger show">Benutzername oder Passwort ist falsch</div>

with my selenium xpath:

WebElement errorMessage = driver.findElement(By.xpath("//*[@id=\"root\"]/div[3]/div/div/div/form/div[1]"));

when I assert the element:

String actualErrorMessage = errorMessage.getText();
assertEquals("Benutzername oder Passwort ist falsch", actualErrorMessage);

I get an assertion error b/c of a different tag text present:

[ERROR] Failures:
[ERROR]   expected: <Benutzername oder Passwort ist falsch> but was: <Passwort vergessen?>
[INFO]
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

How come?

Any ideas appreciated!

I was expecting to reach a different html tag than the actual returned html tag.


Solution

  • //*[@id=\"root\"]/div[3]/div/div/div/form/div[1]
    

    Above XPath expression is poorly constructed. And looks like it is locating some other element(<Passwort vergessen?>).

    Try the below XPath:

    //div[contains(text(),'Benutzername oder Passwort ist falsch')]
    

    or this:

    //div[@role='alert' and @class='fade alert alert-danger show']
    

    With the limited HTML posted in the question, hoping above XPath expression locates the targeted element.