Suresh Madhusanka
Suresh Madhusanka

Reputation: 71

No such element: Unable to locate element: exception coming in selenium

I am getting below error every time when trying to verify the user credentials after login.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//td[contains(text(), 'User: Naveen K')]"}

My code is as follows

Home page test class

@Test(priority = 2)
public void verifyUserNameLabelTest() {

    testUtil.switchToFrame();       

    Assert.assertTrue(homePage.verifyUserName());

}

TestUtil class

public void switchToFrame() {

    driver.switchTo().frame("mainpanel");

}

Home page class

public boolean verifyUserName() {

    return usernameLabel.isDisplayed();

} 

My HTML web page source code is as follows

img

Upvotes: 1

Views: 1527

Answers (2)

iamsankalp89
iamsankalp89

Reputation: 4749

Answer of @rajKamal is nice, but you can also use "." to search the name like that

"//td/font[contains(., 'User: Naveen K')]"}

Upvotes: 0

Raj
Raj

Reputation: 674

Problem is with your xpath, which tries to find user element:

"//td[contains(text(), 'User: Naveen K')]"}

Correct xpath would be:

"//td/font[contains(text(), 'User: Naveen K')]"}

Text which you are looking does not belong to td tag, instead it's belong to font tag.

Upvotes: 4

Related Questions