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– Yu Zhang2018年07月30日 02:05:24 +00:00Commented Jul 30, 2018 at 2:05
-
@YuZhang No, it is not in an iFrame-nchll– nchll2018年07月30日 02:12:40 +00:00Commented 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.2018年07月30日 06:20:36 +00:00Commented Jul 30, 2018 at 6:20
-
@BharatMane- thank you, yes I knew it was going to be something silly...thank you thank younchll– nchll2018年07月30日 15:33:38 +00:00Commented Jul 30, 2018 at 15:33
-
Could you please accept the answer, if it works for you.2018年07月30日 15:49:00 +00:00Commented Jul 30, 2018 at 15:49
-
@BharatMane- I just tried it and it didn't work...it is a cannot locate element error-nchll– nchll2018年07月30日 17:15:56 +00:00Commented Jul 30, 2018 at 17:15
-
Could you please add your HTML code2018年07月30日 17:18:40 +00:00Commented Jul 30, 2018 at 17:18
CSS for the element
#nav-link-accountList span.nav-line-1
XPath for the element
//a[@id='nav-link-accountList']/div/span[@class='nav-line-1']
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.
Explore related questions
See similar questions with these tags.