0

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
 loggedInUsernameText;

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

CSS:

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

XPath:

//*[@id="nav-link-accountList"]/span[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 '18 at 2:05
  • @YuZhang No, it is not in an iFrame- – nchll Jul 30 '18 at 2:12
4

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 '18 at 6:20
  • @BharatMane- thank you, yes I knew it was going to be something silly...thank you thank you – nchll Jul 30 '18 at 15:33
  • Could you please accept the answer, if it works for you. – Bharat Mane Jul 30 '18 at 15:49
  • @BharatMane- I just tried it and it didn't work...it is a cannot locate element error- – nchll Jul 30 '18 at 17:15
  • Could you please add your HTML code – Bharat Mane Jul 30 '18 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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