Selenium with Java 9 - Xpath Locator In Selenium | Types of xpath | Absolute xpath & relative xpath

preview_player
Показать описание
Xpath Locator In Selenium | Types of xpath | Absolute xpath & relative xpath

What is xpath Locator
xpath locator syntax
What is absolute xpath and relative xpath

What is xpath in selenium?
xpath stands for XML Path.
xpath is used to find the element in the HTML DOM.
The success rate of finding an element using xpath is too high.
xpath is used where element has no other way of locating.
xpath provider plugins are available but encouraged to use only to validate the user written xpath.

Types of xpath :
Absolute xpath
Relative xpath

Absolute xpath :
Absolute XPath starts with the root node “html”
The advantage of using absolute is, it identifies the element very fast.
Disadvantage here is, If some other tag added or removed in between, then this path will no longer works.
Example:

If the Path we defined as
1. html/body/div[2]/div[1]/img

If there is a tag that has added between div[1] and img as below
2. /html/body/div[2]/div[1]/p/img

The first path will not work as ‘p' tag added in between

Sample Absolute xpath :
html/body/div[2]/footer/p

Relative xpath :
In relative xpath, path starts with the node of your choice.
It starts with Double forward slash(//).
Syntax: //table/tbody/tr/th
Advantage of using relative xpath are,
You don't need to mention the long xpath
You will choose the element from where the relative path should begin
The chosen element is less likely to change
This is a safer approach.

Sample Relative xpath :
Example 1 : //img
Example 2 : //input[id=‘email’]

xpath to find all elements :
Syntax: .//*

This xpath will select all the elements present in DOM.

xpath using tag name :
Syntax: //tagName

This xpath will select an element whose tagname is input

xpath using attribute :

This xpath will select an element whose tagname is input, attribute is id and value is Email.

xpath using multiple attributes :

This xpath will select an element whose tagname is input, attribute is id is Email and name attribute is also Email

xpath using and :

This XPath will select an element whose tagname is input, id attribute is Email and name attribute is mailid

xpath using or :

This XPath will select an element whose tagname is input, id attribute value is Email or name attribute value is mailid

xpath using index [] :
Syntax://tagname[number]

This xpath will select second td element in second row(tr)

Possible Interview Questions on selenium xpath are :
What is xpath?
Which are the types of xpath?
What are the difference between relative and absolute xpath?
How to select all the elements in DOM using xpath?
How to find element using xpath along with tagname?
How to find element using xpath with attribute?
How to find element using xpath with multiple attributes?
How to find element using xpath with ‘and’ operator?
How to find element using xpath with ‘or’ operator?
How to find element using xpath with indexing?
Рекомендации по теме
Комментарии
Автор

Thank you for watching this video. Cheers! would you mind hitting *_like_* button and *_subscribe_* to our channel!
That will be awesome. We would be grateful to you. And also share this video in your circle via whatsapp, facebook and twitter.

AnkproTraining
Автор

Thnx for sharing your knowledge with us🙏

sahinparwin
join shbcf.ru