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– Yu Zhang2018年07月31日 01:51:17 +00:00Commented 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– nchll2018年07月31日 01:57:28 +00:00Commented 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– lauda2018年07月31日 13:19:15 +00:00Commented 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 -
//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());
-
@NarendraR- thank you- at first it didn't work but I figured out what I did wrong- thank you thank you:)nchll– nchll2018年08月01日 03:10:07 +00:00Commented Aug 1, 2018 at 3:10
Try getText()
on the below xpath:
//a[@id = 'nav-link-accountList']/span
Explore related questions
See similar questions with these tags.