There many locators in selenium.
Based on scenarios we use one of those.
Here are the list of locators:
id, name , css selector, linkText, partialLinkText, Xpath
Sequence as per the choice of faster execution:
1- id
2- name
3- css
4- xpath
5- linkText
6- partialLinkText
Most widely used locators are xpath and css (If id and name is not present)
Between xpath and CSS, css is more faster and more reliable due to below reasons:
1- xpath has different engines for different browsers, where as CSS is same for all browsers
2- CSS is best suited for IE browser than xpath
3- Xpath can traverse backward/froward direction in DOM elements but css can only find forward direction(disadvantage)
- // : Select current node.
- Tagname: Tagname of the particular node.
- @: Select attribute.
- Attribute: Attribute name of the node.
- Value: Value of the attribute.
XPATH examples:
1- //div[@class=’somename’]/table/td
2- //a[text()=’sometext’]
3- //table/tr/td/label[contains(.,’sometext’)]
4- //table/tr/td/label[contains(text,’sometext’)]
5- //*[@type=’submit’ or @name=’btnReset’]
6- //input[@type=’submit’ and @name=’btnLogin’]
<bookshelf>
<book>
<title>The Great Adventure</title>
<author>J.K.Miller</author>
<pages countingCover="true">360</pages>
<publisher>B and B</publisher>
</book>
<book>
<title>On the Way</title>
<author>J.Johnson</author>
<pages countingCover="true">2135</pages>
</book>
<book>
<title>The Silent Rage</title>
<author>Thomas B.</author>
<pages countingCover="false">530</pages>
</book>
</bookshelf>
- //title/ancestor::book – returns all books within its ancestors
- //book/ancestor-or-self::book
- //book/following::book – following nodes after the context node only
- //title/following-sibling::author – following sibling
- //book/child::title
- //title/preceding::book – before book node
- //title/parent::book – all parent nodes
- //book/descendant::title – all parent,child, grandchild below the node
CSS locators:
^ - Starts with
input[id^='cb-select']
$ - Ends with
input[id$='319']
# id
input#cb-select-319
or
input[id='cb-select-319']
. class
input.class_name
* contains
input[id*='login']