Skip to main content

Selenium Xpath

XPath 

XPath is abbreviated as XML Path.  Xpath is used to find the location of any element on the web page using HTML DOM structure.

Basic format of XPath is :
Xpath = //tagnanme[@attribute='value']


  • //  : Select current node.
  • Tagname  : Tagname of the particular node. eg : input, img, div etc
  • @  : Select attribute.
  • Value  : Value of the attribute.

Different locators are used to find the element on web pages accurately.
  • ID 
  • ClassName
  • Name
  • LinkText
  • XPath
  • CSSPath
Types of XPath:
  1. Absolute XPath
  2. Relative XPath

Absolute xpath:

It is direct way to find the element right from the root element of the webpage.
The disadvantage of the absolute xpath is that if there are any changes made in the path of the element then xpath gets failed.

It begins with the single forward slash(/), which means you can select the element from the root node.

example :

html/body/div[1]/section/div[1]/div/div/div/div[1]/div/div/div/div/div[3]/div[1]/div/h4[1]/b

Relative xpath:

XPath can start from the middle HTML DOM structure.
It starts with the '//' , which means it can search the element at the webpage.

example:
//*[@class='featured-box']//*[text()='Testing']


XPath Axes:

Axes are the methods used to find dynamic elements, which otherwise not possible to find element by XPath method using ID, ClassName, Name etc.

Axes methods usually used to fins those elements, which dynamically change on refresh or any other operations.

There are few axes methods commonly used in Selenium Webdriver like child, parent, ancestor, sibling, preceding, self etc.

  • ancestor - select parent or grand parent.
  • following-sibling - which follows.
  • preceding-sibling - which precedes.
  • descendant -  child/SubChild.
  • parent - Select Immediate Parent.
  • child - Select Immediate Child.
  • following - Select all the matching nodes which are following.
  • preceding - Select all the matching nodes which are preceding.
Practical Usage:
//a[contains(text().'Dayananda']/parent::td
//a[contains(text().'Dayananda']/ancestor::tr
//a[contains(text().'Dayananda']/parent::td/following::td


Using Xpath Handling Complex and Dynamic elements in Selenium.

1. Basic XPath:

example :

Xpath=//input[@type='text']    
Xpath= //label[@id='message23']
Xpath= //input[@value='RESET']
Xpath=//*[@class='barone']
Xpath=//a[@href='http://demo.guru99.com/']
Xpath= //img[@src='//cdn.guru99.com/images/home/java.png']

2. Contains():

Contains() is a method used in XPath expression. It is used when the value of any attribute changes dynamically.

The contain feature has an ability to find the element with partial text.

example :

Complete value of attribute 'type' is 'submit' but using only partial value 'sub'

Xpath=//*[contains(@type,'sub')]  
Xpath=//*[contains(text(),'here')]

3. Using OR & AND:

example:
Xpath=//*[@type='submit' OR @name='btnReset']

4. Text():

With text function, we find the element with exact text match.

example:
Xpath=//td[text()='UserID']

Thank You!
Happy Learning

Comments

Popular posts from this blog

Kubernetes ETCDCTL

ETCD is a key-value data store for storing kubernetes state and objects. ETCDCTL is the tool used to back up with snapshot. ETCDCTL a command line tool for interacting with the etcd server  Key Features:  Keep the event history until compaction. access to old version of keys. user controlled key compaction. Support range query. pagination support with limit argument. support consistency guarantee across multiple queries. Replace TTL key with lease. Support watch feature. ETCDTOOL Installation: Build binary from source code  Download tool directly  Build binary from source code: Checkout the code repository git clone -b v3.4.16 https://github.com/etcd-io/etcd.git Change directory to etcd cd etcd Run build script ./build The binaries are under the bin directory. exportPATH="$PATH:`pwd`/bin" Check etcd version etcd --version Download ETCD tool directly: kubectl exec -it etcd-docker-desktop -n kube-system -- /bin/bash -c 'ETCDCTL_API=3 /usr/local/bin/etcd --version' | ...

Kubernetes Helm

Helm Helm is a package manager for deploying applicatons. Adding stable repository helm repo add <alias> <helm repo> helm repo add stable https://charts.helm.sh/stable Search chart in a repo helm search repo stable/mysql Deploy application using helm helm install <Name of the application> <repo/chart-name> Eg: helm install mysql testing/mysql Confirm current context kubectl config current-context Chart definition helm show chart <repo/chartname> helm show readme <repo/chartname> helm show values <chart-name> dry run helm install <name-of -the-application> <chart name>  --dry-run --debug Confirm deployment helm list List pods, services, deployments and replica sets kubectl get all  status of the release helm status <app-deployed> eg: helm status mysql helm get manifest <chart-deployed> View release hhistory helm history mysql Unistall a release helm uninstall mysql --keep-history helm delete mysql Show all the ve...

Ansible

 What is Ansible Ansible is a configuration management, deployment and orchestration tool. Features of Ansible Simple: Simple to install and setup Built on top of Python: Provides lot of python functionality. SSH for Secure Connection  Agentless Platform independent  Push Based unlike Puppet and Chef Install and Setup: Install Ansible via pip install ansible   Generate SSH-Key on Master node using ssh-keygen -t rsa Goto /root/.ssh and copy id_rsa.pub key to all other nodes and  Run ssh-copy-id <Destination IP Address> By default, Ansible configuration file is located under  /etc/ansible/ansible.cfg and inventory file is pointed to  /etc/ansible/hosts ansible --version shows the version of ansible is using and the configuration file it is pointing to. We can create our own inventory file filename - inventory.ini [web_servers]  web01 web02 [db_servers] db01 db02 Create a ansible.cfg file under working directory or edit the default confi...