Note:
- xpath("") --- location paths should be in double quotes
- xpath("//node[@attribute='xcvv']") --- every attribute declaration should be inside [] and value of attribute should be in single quote.
Regular Expression in Xpath??
Following could be used:
ends-with
contains
12/21/2015
Source: http://www.w3.org/TR/xpath/
http://hedleyproctor.com/2011/05/tutorial-writing-xpath-selectors-for-selenium-tests/
Other websites:
http://zvon.org/comp/r/ref-XPath_1.html
3/2/2016:
My Experience:
Chrome
- One could write the xpath by following below process
F12->ctrl+F-> Enter xpath in the field opened.
- To generate xpath, you could go to element, right click on it -> Inspect -> Go to the inspect code and Right click -> Copy the xpath.
You could use this process, when ever you are working on finding element in a web page.
FireFox:
Firebug and Firepath are the two addins that would help you to validate your xpath.
Source:
http://stackoverflow.com/questions/22571267/how-to-verify-an-xpath-expression-in-chrome-developers-tool-or-firefoxs-firebug
- xpath("") --- location paths should be in double quotes
- xpath("//node[@attribute='xcvv']") --- every attribute declaration should be inside [] and value of attribute should be in single quote.
Regular Expression in Xpath??
- We can join two conditions using and
- Use Higlight, adjacent to Xpath section in inspect by Firepath window, when you click Highlight it will show the web element that xpath is refering too. That way you can validate the xpath, whether its pointing out to the proper web element. Dont forget to enter xpath in the field.
- Regular Expression in xpath:
Following could be used:
ends-with
contains
12/21/2015
Source: http://www.w3.org/TR/xpath/
Here are some examples of location paths using abbreviated syntax:
para
selects thepara
element children of the context node*
selects all element children of the context nodetext()
selects all text node children of the context node@name
selects thename
attribute of the context node@*
selects all the attributes of the context nodepara[1]
selects the firstpara
child of the context nodepara[last()]
selects the lastpara
child of the context node*/para
selects allpara
grandchildren of the context node/doc/chapter[5]/section[2]
selects the secondsection
of the fifthchapter
of thedoc
chapter//para
selects thepara
element descendants of thechapter
element children of the context node//para
selects all thepara
descendants of the document root and thus selects allpara
elements in the same document as the context node//olist/item
selects all theitem
elements in the same document as the context node that have anolist
parent.
selects the context node.//para
selects thepara
element descendants of the context node..
selects the parent of the context node../@lang
selects thelang
attribute of the parent of the context nodepara[@type="warning"]
selects allpara
children of the context node that have atype
attribute with valuewarning
para[@type="warning"][5]
selects the fifthpara
child of the context node that has atype
attribute with valuewarning
para[5][@type="warning"]
selects the fifthpara
child of the context node if that child has atype
attribute with valuewarning
chapter[title="Introduction"]
selects thechapter
children of the context node that have one or moretitle
children with string-value equal toIntroduction
chapter[title]
selects thechapter
children of the context node that have one or moretitle
childrenemployee[@secretary and @assistant]
selects all theemployee
children of the context node that have both asecretary
attribute and anassistant
attribute
The most important abbreviation is that
child::
can be omitted from a location step. In effect, child
is the default axis. For example, a location path div/para
is short for child::div/child::para
.
There is also an abbreviation for attributes:
attribute::
can be abbreviated to @
. For example, a location path para[@type="warning"]
is short for child::para[attribute::type="warning"]
and so selects para
children with a type
attribute with value equal towarning
.//
is short for /descendant-or-self::node()/
. For example, //para
is short for /descendant-or-self::node()/child::para
and so will select any para
element in the document (even a para
element that is a document element will be selected by //para
since the document element node is a child of the root node); div//para
is short for div/descendant-or-self::node()/child::para
and so will select all para
descendants of div
children.NOTE: The location path//para[1]
does not mean the same as the location path/descendant::para[1]
. The latter selects the first descendantpara
element; the former selects all descendantpara
elements that are the firstpara
children of their parents.
A location step of
.
is short for self::node()
. This is particularly useful in conjunction with //
. For example, the location path .//para
is short forself::node()/descendant-or-self::node()/child::para
and so will select all
para
descendant elements of the context node.
Similarly, a location step of
..
is short for parent::node()
. For example, ../title
is short for parent::node()/child::title
and so will select the title
children of the parent of the context node.
More examples are covered in the following website
http://hedleyproctor.com/2011/05/tutorial-writing-xpath-selectors-for-selenium-tests/
There are a number of ways to write and test an XPath expression:
- Using the Selenium IDE itself – it has a “test” option.
- Using Firebug and a separate XPath tester.
- Using Firebug and the FirePath tester, which is a separate add-on but integrates with Firebug.
Other websites:
http://zvon.org/comp/r/ref-XPath_1.html
3/2/2016:
Ways of Validating Xpath:
xpath could be validated or written on own to find web objects in web pages. And process of writing and finding xpath is possible in many ways, for an example in
My Experience:
Chrome
- One could write the xpath by following below process
F12->ctrl+F-> Enter xpath in the field opened.
- To generate xpath, you could go to element, right click on it -> Inspect -> Go to the inspect code and Right click -> Copy the xpath.
You could use this process, when ever you are working on finding element in a web page.
FireFox:
Firebug and Firepath are the two addins that would help you to validate your xpath.
Source:
http://stackoverflow.com/questions/22571267/how-to-verify-an-xpath-expression-in-chrome-developers-tool-or-firefoxs-firebug
1. Chrome
This can be achieved by three different approaches (see my blog article here for more details):
- Search in
Elements
panel - Execute
$x()
and$$()
inConsole
panel, as shown in Lawrence's answer - Third party extensions (not necessary)
Here is how you search XPath in
Elements
panel:- Press F12 to open Chrome Developer Tool
- In "Elements" panel, press Ctrl+F
- In the search box, type in XPath or CSS Selector, if elements are found, they will be highlighted in yellow.
2. Firefox
- Install Firebug
- Install Firepath
- Press F12 to open Firebug
- Switch to
FirePath
panel - In dropdown, select XPathor CSS
- Type in to locate
3. Chrome and Firefox
You can open a Console in Chrome, and check the XPath by typing
$x("your_xpath_here")
. This will return an array of matched values. If it is empty, you know there is no match on the page.