I am teaching myself automation with Selenium web driver, and thought a good way was to learn off an ecommerce site such as Amazon. I am trying to locate inner text with CSS and used the following to verify logged in user name via page factory:

Locating Element with CSS:

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

Error Message:

org.openqa.selenium.InvalidSelectorException: invalid selector: 
An invalid or illegal selector was specified

Unfortunately this did not work for me, so I tried to use XPath:

@FindBy (how=How.XPATH, using= "//*[contains(text(),('Hello,Nichole')]") WebElement loggedInUsernameText;

Error Message:

    org.openqa.selenium.InvalidSelectorException: invalid selector: 
    Unable to locate an element with the xpath expression //*[contains(text(),('Hello,Nichole')] because of the following error:
    SyntaxError: Failed to execute 'evaluate' on 'Document': 
The string '//*[contains(text(),('Hello,Nichole')]' is not a valid XPath expression.

This didn't work for me either- can someone please tell me what I am doing wrong?

Here is the locator for CSS and Xpath:

Hello, Nichole


#nav-link-accountList > span.nav-line-1



I am probably doing something really dumb! Let me know if I need to provide other information- oh to find it on the console it does specify to use $0- which I did use and it located it, so I am guessing I need to implement that somehow?

  • the element you are looking for is in an iFrame?
    – Yu Zhang
    Jul 30, 2018 at 2:05
  • @YuZhang No, it is not in an iFrame-
    – nchll
    Jul 30, 2018 at 2:12

2 Answers 2


It's Pretty easy. Look at your XPath expression and the Syntax error:

You have added unwanted bracket "()" in your XPath.

Your XPath should be:

//*[contains(text(), 'Hello,Nichole')]

Using Page-Factory:

@FindBy (how=How.XPATH, using= "//*[contains(text(), 'Hello,Nichole')]")
 WebElement loggedInUsernameText;
  • It should definitely work, Let me know if you still have any problem with this.
    – Bharat Mane
    Jul 30, 2018 at 6:20
  • @BharatMane- thank you, yes I knew it was going to be something silly...thank you thank you
    – nchll
    Jul 30, 2018 at 15:33
  • Could you please accept the answer, if it works for you.
    – Bharat Mane
    Jul 30, 2018 at 15:49
  • @BharatMane- I just tried it and it didn't work...it is a cannot locate element error-
    – nchll
    Jul 30, 2018 at 17:15
  • Could you please add your HTML code
    – Bharat Mane
    Jul 30, 2018 at 17:18

CSS for the element

#nav-link-accountList  span.nav-line-1

XPath for the element


You can modify the @FindBy section for CSS

 @FindBy (how=How.CSS, using= "#nav-link-accountList  span.nav-line-1") 
 WebElement loggedInUsernameText;

Also you could shorten the element definition

@FindBy(css = "#nav-link-accountList  span.nav-line-1")
WebElement loggedInUsernameText;

Mastering XPath and Mastering CSS for Selenium test automation engineers could be useful.

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.