Site Loader
Auckland, New Zealand
In this post we will discuss understanding and working with Xpath and how to use the same in selenium.

What is Xpath

  • Xpath is a syntax for defining parts of an XML documents
  • Xpath is major element in XSLT
We are not going to deal with the theory of Xpath, which you can always get from lot of website and the best of them all is w3school.

Working on xpath

  • Mostly while working with some application which has less/no unique identification property, we use Xpath majorly.
  • Xpath looks something like this
    • .//*[@id=’fbt_x_check’]/following-sibling::div
    • .//*[@id=’feature-bullets’]/ul/li[3]/span/span/div/tr/td[4]
    • .//*[@id=’feature-bullets’]/ul/li[3]/span/span/div/tr/td[4]/td/[7]/table/div/li/tr/

Locating element with xpath

Xpath can be formed either by hand coding, if you have good knowledge on the structure of the page or by using tools like Firepath etc

Some common Xpath tidbits

Xpath Meaning
/ Selects from the root node
// Selects nodes in the document from the current node that match the selection no matter where they are
//*[@id=’fbt_x_check’] Selecting with the attribute
//*[@id=’sims_fbt’]/descendant::div Selecting all descendant divs from current node
//*[@id=’sims_fbt’]/descendant::div[last()] Selecting the last div from the current node which is descendant (Used Predicate as well !!!)
//*[@id=’fbt_x_check’]/following-sibling::div Selecting next sibling from current node
  Here is the complete video of the above discussion Thanks for watching the video !!! Please let me know if you have any questions from the above video. Thanks, Karthik KK

Post Author: Karthik kk

One Reply to “Understanding and working with Xpath”

Leave a Reply to chippa rakesh Cancel reply

Your email address will not be published. Required fields are marked *