How to Find XPath on the latest versions Firefox (56+):

With the release of Firefox version 56+, Firepath & Firebug becomes legacy addons and no longer supported. We can no longer use this addons to construct the XPath.

For the latest of Firefox, all the capabilities of Firebug are now present in current Firefox Developer Tools. With the Developer Tool of Firefox, we can now easily identify the XPath.

 Assume we need to find the XPath of the Email or Phone text box of Facebook login page.

Right click on the field as shown below and click Inspect Element.

Once you clicked, it will open the inspector view as shown below and will highlight the html text box which is tag input.

Now right click on the html, it will display context menu with copy options. Click the XPath from the sub menu.

It will automatically construct the XPath as //*[@id=”email”]. You can directly copy & use this XPath in automation code.

Firefox constructs the XPath based on id, in case ID is generating dynamically we can’t use the XPath in our automation code.

If ID is not present as attribute for the html element, then it will generate the absolute XPath which can breakup in the long run.

How TruePath Help you Construct XPath

It creates the multiple XPath, for the target field email & Phone.

From the multiple option, that the TruePath generates, one can easily select the suitable one. Since it generates the relative XPath, the possibility of broken down of the XPath is low. Once selected, it will provide the format options to select from either in the normal or page object format.

For more details on TruePath click here
 
To check or construct XPath manually, one can click console of the developer tool of the Firefox browser as shown below

The most comprehensive Selenium WebDriver cheat sheet dedicated to the locators

XPath

Description

//label

label element

//label[@id=’Id’]

label element with @id= ‘Id’

//label[@id!=id]

label elements with @id not equal to ‘id’

//input[@name]

input elements that have name attribute

//*[contains (@id, ‘Id’)]

element with @id containing

//*[starts-with(@id, ‘Id’)]

element with @id starting with

//*[ends-with(@id, ‘Id’)]

element with @id ending with

//*[matches(@id, ‘r’)]

element with @id matching regex ‘r’

//*[@name=’Name’]

image element with @name= ‘Name’

//*[@id=’X’ or @name=’X’]

element with @id X or a name X

//*[@name=”N”][@value=”v”]

element with @name N & specified @value ‘v’

//*[@name=”N” and @value=”v”]

element with @name N & specified @value ‘v’

//*[@name=”N” and not(@value=”v”)]

element with @name N & not specified @value ‘v’

//input[@type=”submit”]

input of type submit

//section[//h1[@id=’hi’]]

returns <section> if it has an <h1> descendant with @id= ‘hi’

//table[count(tr) > 1]

return table with more than 1 row

//*[.=”t”]

element containing text ‘t’ exactly

//a[contains(text(), “Log Out”)]

anchor with inner text containing ‘Log Out’

​//a[not(contains(text(), “Log Out”))]

anchor with inner text not containing ‘Log Out’

//a[@href=”url”]

anchor with target link ‘url’

//img/*[1]

first child of element img

//ul/child::li

first child ‘li’ of ‘ul’

//img[1]

first img child

​//img/*[last()]

last child of element img

//img[last()]

last img child

//img[last()-1]

second last img child

//input/following-sibling::a

‘a’ following some sibling ‘input’

//a/following-sibling::*

//input/preceding-sibling::a

sibling element immediately following ‘a’

‘a’ preceding some sibling ‘input’

//input/preceding-sibling::*[1]

sibling element immediately preceding ‘input’

//img[@id=’MyId’]::parent/*

the parent of image with id

//*[@id=”TestTable”]//tr[3]//td[2]

cell by row and column

//td[preceding-sibling::td=”t”]

cell immediately following cell containing ‘t’ exactly

//td[preceding-sibling::td[contains(.,”t”)]]

cell immediately following cell containing ‘t’

//input[@checked]

checkbox (or radio button) that is checked

//a[@disabled]

all ‘a’ elements that are disabled

//a[not(@disabled)]

all ‘a’ elements that are not disabled

//a[@price > 2.70]

‘a’ with price > 2.7

//ul[*]

All the children of uL