0

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-

asked Jul 31, 2018 at 1:27
3
  • Someone asked the same question, are you guys from the same class? working on the same assignment? Commented 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- Commented 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. Commented Jul 31, 2018 at 13:19

2 Answers 2

1

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 -

 //a[@id='nav-link-yourAccount']/span[contains(.,'Nichole')]

e.g.

 @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 -

#nav-link-yourAccount>span:nth-of-type(1)

e.g.

 @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

 System.out.println(loggedInUsernameText.getText());
answered Jul 31, 2018 at 5:18
1
  • @NarendraR- thank you- at first it didn't work but I figured out what I did wrong- thank you thank you:) Commented Aug 1, 2018 at 3:10
0

Try getText() on the below xpath:

//a[@id = 'nav-link-accountList']/span
answered Jul 31, 2018 at 4:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.