Types of Element Locators:
1| Html id | id attribute
2| Html name | name attribute
3| XPATH
4| Class name
5| CSS Selector
6| Link Text
7| Tag Name
XPATH
XPath is a way to navigate in xml document and this can be used to identify elements in a wen page. You may have to use XPath when there is no name/id associated with element on page or only partial part of name/IDE is constant.
Direct child - /
Relative child - //
id, class, names can also be used with XPath
//input[@name='q']
//input[@id='q']
//input[@class='q']
e.g., By.xpath(“//input[@id=’myElementId’]”)
CSS Selector
CSS location strategy can be used with Selenium to locate elements, it works using cascade style sheet location methods.in which -
Direct child is denoted with - (a space)
Relative child is denoted with - >
id, class, names can also be used with CSS
css=input[name='q']
css=input[id='q'] or input#q
css=input[class='q'] or input.q
e.g., By.cssSelector(“h1[title]”)
Link Text
e.g.,
By.linkText(“your text”)
By.partialLinkText(“your text”)
Note:- If there are constant name/id available, then they should be used instead of XPath and CSS locators. If not, then CSS locators should be given, as their evaluation is faster than XPath in most modern browsers.