Locators & Object Identification
• Tools to identify elements
• Firebug
• IE Developer tools
• Google Chrome Developer tools
• Finding elements by ID
• Finding elements by name
• Finding elements by link text
• Finding elements by XPath
• Finding elements by AbsoluteXPath
• Finding Elements by Class Name
• Finding Elements by CSS selector
Tools to identify elements
Firefox – Selenium IDE
Firefox – Firebug
Firefox – Firepath
Google Chrome Developer Tools
IE Developer tool bar
fire-ie-selenium
XPath Selenium Selectors
• Syntax = //tagname[@attribute=’Value‘]
• Example = //input[@id=’user-message‘]
Absolute and Relative XPath
The difference between absolute and relative Xpath
• Absolute XPath
• It is a direct way to locate an element.
• Starts with single slash “/” that means starting to search
from the root.
• Relative XPath
• Starts from the middle of the HTML DOM.
• Starts with a double slash “//” that means it can start to
search anywhere in the DOM structure.
• Shorter than Absolute XPath.
Smart XPaths for Complex and Dynamic Elements
• 1) Writing XPath with Tag & Attribute & Value
Syntax: //tag[@attribute=’value‘]
• Examples:
Xpath = //input[@type=’send text’]
Xpath = //label[@id=’clkBtn’]
Xpath = //input[@value=’SEND’]
Xpath = //*[@class=’swtestacademy’]
–> “*” means, search “swtestacademy” class for all tags.
Xpath = //a[@href=’https://www.swtestacademy.com/’]
Xpath =
//img[@src=’cdn.medianova.com/images/img_59c4334feaa6
d.png’]
2) Writing XPath with contains()
• Syntax: //tag[contains(@attribute, ‘value‘)]
Example: //input[contains(@id, ‘er-messa’)]
• Xpath = //*[contains(@name,’btnClk’)]
–> It interrogates “btnClk” for all name attributes in the
DOM.
Xpath = //*[contains(text(),’here’)]
–> It interrogates the text “here” in the DOM.
Xpath = //*[contains(@href,’swtestacademy.com’)]
–> It interrogates “swtestacademy.com” link in the DOM.
3) Writing XPath with starts-with
• Syntax: //tag[starts-with(@attribute,
‘value‘)]
Example: //input[starts-with(@id, ‘user’)]
4) Writing XPath with Chained
XPaths Declerations
• multiple relative XPath declarations with “//”
double slash to find an element location as
shown below.
• Example: //div[@class=’form-group’]//
input[@id=’user-message’]
5) Writing XPath with “or” Statement
• Syntax: //tag[XPath Statement-1 or XPath
Statement-2]
• Example: //*[@id=’user-message’ or
@class=’form-control’]
“or” is case-sensitive
6) Writing XPath with “and“ Statement
• Syntax: //tag[XPath Statement-1 and XPath
Statement-2]
• Example: //*[@id=’user-message’ and
@class=’form-control’]
“and” is case-sensitive.
7) Writing XPath with text()
• Syntax: //tag[text()=’text value‘]
• Example: .//label[text()=’Enter message’]
8) Writing XPath with ancestor
.//*[@class=’container-fluid’]//ancestor::div[2]
9) Writing XPath with following
• .//form[@id=’gettotal’]//following::input
10) Writing XPath with child
• //nav[@class=’fusion-main-menu’]//
ul[@id=’menu-main’]/child::li
11) Writing XPath with preceding
• //img[contains(@src,’cs.mailmunch.co’)]//
preceding::li
12) Writing XPath with following-sibling
• //*[@class=’col-md-6
text-left’]/child::div[2]//*[@class=’panel-
body’]//following-sibling::li
13) Writing XPath with Descendant
• //nav[@class=’fusion-main-menu’]//
*[@id=’menu-main’]//descendant::li
14) Writing XPath with Parent
• .//*[@id=’get-input’]/button//parent::form
15.Get Only One Specific Element from
Multiple Elements with XPath
• //span[contains(text(),’odamax’)]
• Above XPath returns many Odamax hotel’s, we can
select the first one with below XPath expression:
• (//span[contains(text(),’odamax’)])[1]
• To search and find the related hotel’s price
element with below XPath:
• (//span[contains(text(),’odamax’)])[1]/following-
sibling::strong[@class=’deals__price’]