I am new to automation, Selenium Web Driver and am practicing code using Amazon. I want to verify my logged in user name (Hello, Nichole) on the Amazon Home Page but anything I enter using CSS or Xpath comes back in error. I am also using page factory-

    //@FindBy (how=How.CSS, using= "a:contains'Hello,Nichole')") WebElement loggedInUsernameText;

I am not going to add my xpath expression because I tried too many variations, to no avail.

I have attached a screenshot of the HTML code-Hello,Nichole HTML CODE

Any help would be appreciated- let me know if I need to supply more info-

  • Someone asked the same question, are you guys from the same class? working on the same assignment?
    – Yu Zhang
    Jul 31, 2018 at 1:51
  • @YuZhang- no its the same person, I thought maybe I didn't explain it well the first time so I re-posted it...still waiting for a response :( I'll remove my previous question so I don't cause confusion-
    – nchll
    Jul 31, 2018 at 1:57
  • Why not simply wait for that selector and after check that contains your username/name ? this is a better option.
    – lauda
    Jul 31, 2018 at 13:19

2 Answers 2


The way you are using cssSelector to find an element using some text, It doesn't allow in cssSelector. There is no such method in CSS selector to locate an element using text so for that you have to use xpath locator as below -



 @FindBy (how=How.XPATH, using= "//a[@id='nav-link-yourAccount']/span[contains(.,'Nichole')]")
 WebElement loggedInUsernameText;

I would recommend the below CSS path to locate the same -



 @FindBy (how=How.CSS, using= "#nav-link-yourAccount>span:nth-of-type(1)")
 WebElement loggedInUsernameText;

And then you can use the getText() method to grab the text

  • @NarendraR- thank you- at first it didn't work but I figured out what I did wrong- thank you thank you:)
    – nchll
    Aug 1, 2018 at 3:10

Try getText() on the below xpath:

//a[@id = 'nav-link-accountList']/span

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.