just //t
if you want all <t>
tags
W3Schools has really good free courses regarding everything that is HTML related. I highly recommend reading it and making the examples. https://www.w3schools.com/xml/xpath_intro.asp
HINT: you can use your browser's console to evalute expressions. It's under Developer Tools - F12 key for Chorme and Firefox:
$x('<your expressions>');
So, as everyone said, you can use the //
syntax to find an element anywhere in the page.
e.g. //a
will return you all a
elements within the page.
Most likely you will want a specific one, so, that's where you use the predicates. They are contained between brackets. Using this very page as example, here is a xquery:
//a[text()="Simeon"]
This xpath will return all a
elements that have Simeon as it's text.
In many situations you might need to improve you xpath to include more identifiers, be more specific.
//a[text()="Simeon" AND @href="/users/274344/simeon"]
So, you can use pretty much any HTML attribute, or even CSS to identify a specific node you want.
But now, let´s take it up a notch.
Let´s say you want to get the a
element that is below user's mkimd answer from Jan 27.
If you look at this page structure, you have to get you a
and dive back a few levels, untill you are able to reach the span that holds the posting date.
//a[text()="mkimd" AND ../../div[@class="user-action-time"]/span[contains(.,"Jan")]]
There are many ways of doing these queries,this last example I gave can be achieved with different xqueries.
I find that xqueries are very similar to navigation in directories in console, like Linux BASH - relative and absolute paths, and the identifiers are like SQL WHERE
clauses.
If you research, there are many functions available in XPATH syntax, such as
+
,-
,*
,div
,!=
,<
,<=
,>
, ...)I highly advise you to use some tool like Firefox Firefug FirePath addon to practice xquery and check if you are getting the element you want - it highlights the elements found.
====================
EDIT - May-8th-15
In case you are using Xpath in Selenium automation, know that it will not help you select CSS pseudo elements.
You can use //
to select all nodes from the current node. So //text()
would select all text nodes.
If you wanted all t elements you would do //t
. If you wanted to do do all t
elements from a certain point you might then do /x/y//t
.
You are looking for the descendant axis:
the
descendant
axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace nodes
In your case: /descendant:t
Of course, as others have answered, there is an abbreviated syntax for this:
//
is short for/descendant-or-self::node()/
. For example,//para
is short for/descendant-or-self::node()/child::para
and so will select anypara
element in the document (even apara
element that is a document element will be selected by//para
since the document element node is a child of the root node)