Session 24 – Selenium with Java | Locators – XPath | XP… — Transcript

Learn about XPath locators in Selenium with Java, including XPath definition, DOM relation, and types like absolute and relative XPath.

Key Takeaways

  • XPath is the most commonly used locator in Selenium automation.
  • XPath depends on the DOM structure which must be fully loaded to work correctly.
  • Absolute XPath is a full path from the root node, while relative XPath is a partial path.
  • Relative XPath is generally preferred for its flexibility and maintainability.
  • Understanding DOM and XPath relationship is essential for effective element location.

Summary

  • Introduction to XPath as a crucial locator in Selenium automation, used 80-90% of the time.
  • XPath is derived from XML and represents the address of an element on a web page.
  • XPath works based on the Document Object Model (DOM) loaded at runtime in the browser.
  • Explanation of DOM as the structure loaded when a web page is browsed and inspected.
  • XPath can only locate elements after the DOM is fully loaded; otherwise, it may cause errors.
  • Two main types of XPath: absolute (full) XPath and relative (partial) XPath.
  • Absolute XPath starts from the root HTML node and navigates through every node to the target element.
  • Relative XPath is a shorter, partial path to the element, often more robust and preferred.
  • Demonstration of how to copy both absolute and relative XPath from browser developer tools.
  • Detailed breakdown of absolute XPath structure, showing navigation through HTML, body, header, and div tags.

Full Transcript — Download SRT & Markdown

00:04
Speaker A
So today, we'll see XPath locator. The last classes, we have seen the basic locators like ID, name, link text, partial link text, tag name, and class name. Then we also discussed CSS selector and different combinations of CSS selector. Today, we'll see one more type of locator, which is XPath, and it is most important. Most of the times, we prefer to use XPath. 80 to 90% of the times, we always use XPath to locate the element, even in automation and real-time projects and almost everywhere. Let's see what exactly XPath means and what it contains, how we can write an XPath, how we can generate an XPath, and what are the different types of XPath we can create. There are multiple things we have to discuss in this. Okay, let us start. So the basic definition, what is an XPath? XPath is nothing but it is derived from the XML path. Basically, the XPath works based on XML. It is derived from the XML language. XML is an extensible markup language. It is just like an HTML. XML is also another type of a scripting language. From XML, XPath is created or designed. In short, we can call it as XPath. And what is the definition of the XPath? XPath is an address of the element. XPath is representing an address of the element. That means, if there is an element on the web page, it can be anywhere throughout the web page. It can be placed anywhere. There should be some address for that element. Okay, for every element, there should be some address, and that address we can capture. That is called XPath, address of the element. XPath is representing an address of the element. Okay, from where will we get that XPath? XPath works based on the DOM. DOM is nothing but a Document Object Model. So Document Object Model, this is the full form of DOM. So let us try to understand what is a DOM. DOM is nothing but a Document Object Model which is created when you browse the web page at runtime. Okay, so normally what is happening is, how can we browse the pages? You will open the browser and you will type some URL like this. So as soon as you type URL or as soon as you hit something, at runtime, it will load the DOM structure. So now you got a page. So if you try here, you can say inspect. So whatever you can see here, this is called DOM. Okay, so here in the elements, you can see, right? This is called DOM, Document Object Model. Okay, so this Document Object Model, this DOM will be loaded at runtime once you browse any web page. And based on this DOM, the XML or XPath will work. Also, we can capture XPath from this particular Document Object Model. Okay, so XPath is an address of the element and we can capture the XPath from the DOM itself. Okay, sometimes what happens is you have created some XPath locator in your automation script and you have executed, and you may get some kind of exception like DOM is not loaded or something like that. So what does it mean? XPath will be able to capture from the DOM, but when you are browsing the page, at the time DOM is still not fully loaded. So once this DOM is fully generated by the browser, then only XPath will be able to capture the element or XPath will automatically work. If the DOM is still not loaded completely, even if the XPath is correct, it cannot identify the element and that cannot be found in the DOM. So DOM should be completely loaded and then only XPath will work, right? So Document Object Model, XPath will work based on the DOM, Document Object Model. Now there are two kinds of XPath, and somebody's writing, just a second, okay, just a moment. All right, so XPath is an address of the element. DOM is a Document Object Model. Based on the DOM, XPath will work. Okay, now there are two kinds of XPath we have. Let us see what are those two types of XPaths. Two types of XPath. Okay, one is called absolute XPath. Absolute XPath, and this is also called full XPath. Second type of XPath is called relative XPath, which is also called partial XPath. Okay, absolute XPath and partial XPath. Absolute XPath means what? It is also called full XPath. Relative XPath is nothing but partial XPath. These are the two kinds of XPath we have. So let me show you how these XPaths look like. Suppose I just open some application here. Any application you can just open. Suppose I want to find XPath of this input box. Okay, so you can just right-click on it and you can simply say, here you can say inspect that element. So here input is a tag name. There are some more attributes there, right? So if you want to capture XPath from the dev tools, simply you can right-click on this statement and here you can see copy, and here you can see two options: copy XPath, copy full XPath. So when I say copy full XPath and then paste it over here, this is called absolute XPath. Okay, now let me show you relative XPath. So for the same statement, again I'm right-clicking here, go to copy, and then say copy XPath, just copy XPath. And then you can see XPath like this. So this is a relative XPath and this is an absolute XPath. This is an example for absolute XPath. This is the example for the relative XPath. So there are two kinds of XPath which we have, one is called absolute XPath. This is an example. I'll discuss all the stuff, differences, how we can generate all those things, just to have some time. Okay, this is a relative XPath. Okay, so this is absolute XPath and this is the relative XPath. Okay, now if you look at these two XPaths, we can see a lot of difference. So in absolute XPath, if you look at here, this XPath is got generated. What exactly they are? If you look at this, what is this? HTML, body, header, div, div, input. These are all tags. These are all tag names. Okay, if you look at this HTML, you can see HTML, header, body, right? And these are all multiple tags are there. And here there is an input. So absolute XPath contains only the tag names. But where exactly our element is present, this is our exact element which is having input tag, right? That is our exact element we are looking for. But apart from this tag, it also captured other tag names which are there in the previous nodes. That means, if you look at the absolute XPath, this is the actual element, but from where it is got generated? From the HTML, then it goes to body, then header section. Inside this, there are divs. Inside this, another div. Inside this, another div. Again here, there are multiple divs. So it goes to second div, again one more div, and here we can find the input tag. That means absolute XPath will be created by navigating each and every node till you reach the expected node. For example, let's say here input is tag. So what is the root node here? It is HTML. We are just navigating from the beginning. So absolute XPath always starts from the root HTML node. We can call it every tag is a node. A node is representing one element. So HTML is a root node, and inside this, we go to body. Inside the body, we go to header. Inside the header, there is one div tag. If I look at this XPath, you can just compare like this. So first HTML, then body, then header. So inside the header, there is only one div tag here. So there is one div. Now if we expand this div tag a little bit like this, again there are two div tags, there are three div tags inside the div. So this is a header. Header contains one div tag. Inside this div tag, there is one more div tag. And you can look at here, this is a main div tag under header, and inside this, there is another div tag. Now if you expand this div tag again, there are three div tags. Now in which div tag is available our element? In the second div tag. So we need to expand the second one, right? So here we can say index div of two. And inside this, there is only one div tag, and this div tag contains an input tag. So here, which contains only one div tag which contains the input. So if you look at this absolute XPath, absolute XPath will navigate from the root HTML. So till it reaches the element, it will capture every node or every tag between the HTML and your element. Between these two, whatever the nodes are available in the hierarchy, basically it will follow the...
00:25
Speaker A
of uh locator which is xath and most important and most of most of the times we prefer to use xath 80 to 90% of the times we always use xath to locate the element even in Automation and realtime
00:40
Speaker A
projects and almost everywhere uh let's see what exactly xath means and what it contains so how we can write an xath how we can generate an xath and what are the different types of xath we can create there are multiple things we have to discuss in this okay let us start so the basic definition
00:59
Speaker A
what is an exp X path is nothing but it is derived from the XML path basically the X path Works based on the XML it is derived from the XML language XML is a extensible markup language it is just like
01:15
Speaker A
an HTML XML is also another type of a scripting language from XML xath is got created or designed in shortcut in XML path in shortcut we can call it as a x paath and what is the definition of the
01:29
Speaker A
xth X paath is an address of the element xath is representing an address of the element means what if there is an element on the web page it can be anywhere on the throughout the web page it can be
01:44
Speaker A
placed anywhere there should be some address for that element okay for every element there should be some address and that address we can capture that is called xath address of the element xath is representing an address of the element okay from where we will get that xath xath works based
02:05
Speaker A
on the Dom Dom is nothing but a document object model so document object model this is the full form of Dom so let us try to understand what is a Dom Dom is nothing but a document object model which is got created when you browsing the web page at the run time okay so normally what
02:30
Speaker A
is happening is how we can browse the pages you will open the browser and he will you will type some URL like this so as soon as you type URL or as soon as you hit something and at the run time
02:42
Speaker A
it will load the Dom structure so now you got a page so if you try here and you can say inspect so whatever you can see here this is called Dom okay so here in the elements you can see right
02:56
Speaker A
this is called Dom d document object model okay so this document object model this Dom will be loaded at the run time once you browse any web page and based on this Dom the XML or xath will work also
03:13
Speaker A
we can capture xath uh from this particular do docment object model okay so xath is an address of the element and we can capture the xath from the Dom itself okay sometimes what happens is you have created some xath locator in your automation script and you have executed and you may get some
03:36
Speaker A
kind of exception like Dom is not loaded something like that so what does it mean is that xath will able to capture from the Dom but when you browsing the page at the time Dom is still not fully loaded
03:51
Speaker A
so once this Dom is fully generated by the browser then only xath will able to capture it the element or xath will automatically works if the Dom is still not loaded completely so even the xath is correct it cannot identify the element and that cannot be find in the Dom so Dom should
04:13
Speaker A
be completely loaded and then only xath will work right so document object model so xath will work based on the Dum document object model now there are two kinds of xar and somebody's writing just a second okay just a
04:50
Speaker A
moment all right so xath is an address of the element Dom is a document object model based on the Dom xath will work okay now so there are two kinds of X paath we have let us see what are
05:13
Speaker A
those two types of X paths two types of X path okay one is called absolute X path absolute X path and this is also called as full X path second type of X path is called relative X path which is
05:37
Speaker A
also called partial X paath okay absolute X paath and partial X path absor X paath means what is also called as full X paath relative X paath is nothing but a partial X paath these are the two kinds of X paath we have so let me show you how these xarts looks like suppose I just open open
06:00
Speaker A
some application here any application you can just open suppose I want to find xath of this input box okay so you can just right click on it and uh you can simply say uh here you can say inspect that element so here input is a tag name there are some more attributes are
06:24
Speaker A
there right so if you want to capture xath from the dev tools simply you can write click on this statement and here you can see copy and here you can see two options copy xath copy full X paath
06:39
Speaker A
so when I say copy full X paath and then paste it over here this is called absolute xath okay now let me show you relative xath so for the same statement again I'm right clicking here go to copy and then say copy xath just copy xath and then you can see xath like this so this is a
07:04
Speaker A
relative xath and this is a absolute xath this is an example for Absol xath this is the example for the relative xath so there are two kinds of xath which we have one is called absor X path this is
07:20
Speaker A
an example I'll discuss all the stuff differences how we can generate all those things just to have some time okay this is a relative X path okay so this is Absol X path and this is the relative X
07:37
Speaker A
path okay now if you look at these two x paaths we can see lot of difference so in absolute xath if you look at here this exper is got generated what exactly they are if you look at this what is this
07:53
Speaker A
HTML body header d d input these are all tab these are all tag names okay if you look at this HTML you can see HTML header body right and these are all multiple T tags are there and here there is a
08:11
Speaker A
input so absolute xath contains only the tag names but where exactly our element is present this is our EXA element which is having input tag right that is our exact element we are looking for but apart from this tag it is also captured other tag names which are there in the previous nodes means
08:34
Speaker A
what if you look at the absolute X path this is the actual element but from where it is got generated from the HTML then it go to body then header section inside this there are D inside this another D inside this another D again here there are multiple DS are there so it is go to
08:53
Speaker A
Second D again one more D and here we can find the input act that means Absol X paath will be created created by navigating each and every node till you reaches the expected node for example let's say here input is tag so what is the root node here is HTML we are just navigating from the beginning
09:14
Speaker A
so Absol xath is always start from the root HTML node we can call it every tag is a node okay node is representing one element so HTML is a root node and inside this we go to body inside the body we
09:30
Speaker A
go to header and inside the header there is one D tag if I look at this uh X paath you can just compare like this so first HTML then body then header so inside the header there is only one D
09:46
Speaker A
tag here so there is a One D now if we expand this D tag little bit like this again there is two D tags are there three D tags are there inside the D so this is a header header contains One D tag
10:02
Speaker A
inside this D tag there is one more D tag and you can look at here this is a main D tag under header and inside this there is another D tag now if you expand this D tag again there are three D tags
10:15
Speaker A
now in which D tag is available our element in the second D tag so we need to expand the second one right so here we can say index D of two and inside this there is only one D tag and this D tag
10:30
Speaker A
contains a input tag so here which contains only one du tag which contains the input so if you look at this absolute xath observed xath will navigate from the root HTML so till it reads the element it will capture every node or every tag between the HTML and your element between these two
10:55
Speaker A
whatever the nodes are available in the hierarchy basically it will follow the hierarchy structure in that particular tree or hierarchy model it will capture each and every tag or node till it reaches the element and Absol X paath contains only tag names okay so this is a Absol X paath
11:14
Speaker A
it is starting from the HTML node navigated each and every tag in the hierarchy and then finally it reaches the element here this is called absolute X paath now come to the relative X path relative X path will directly jump to the element because in the relative X path we no need to navigate
11:39
Speaker A
from the root node we will directly jump to the element by using some attribute so what is the major difference between absolute and relative xath is absolute xath we never specify any attributes we never use any attributes here just we use only tag names of the elements but in the
11:58
Speaker A
relative X path we use something called attributes we can use single attribute or we can use multiple attributes there are multiple options are there but we will use mainly attribute of the element and here star star representing a node star star uh sorry SL SL star is representing the tag name
12:19
Speaker A
or instead of star we can put the tag name also no problem and attribute and first it will go to search and in that you can find the input tag suppose if you look at here this is input and
12:30
Speaker A
where is this input tag is there in the parent so this div is a parent so input is not having any ID here so do we have any ID attribute here no so it is taken parent element by using ID it
12:45
Speaker A
is create xath for this inside this there is input tag so you can simply say slash input we can also write direct xath for input if you want to write simply can specify the attribute so name equal to
12:57
Speaker A
search is there so instead of ID you can directly say name equal to search and you can remove this part this is called relative xath okay this is actual relative xath we just use some attribute of the element inside the xath and what is the major difference here is absolute X paath will
13:19
Speaker A
start from the beginning and each and every node it will capture till it reaches the final node and relative X paath by using the this attribute it will directly jump to the element and then it will find out it will not navigate from each and every node okay so this is an example for absolute
13:40
Speaker A
xath this is an example for the relative xath so if you want to capture from the dev tools you can simply inspect that element go to the statement you can right click and copy and then use these two options and suppose if you want to capture this by using selector Hub then how we can do
14:00
Speaker A
it there are direct options are available you can just right click on the element go to selector Hub here and here you can see direct options so ID you can copy directly name you can copy relative xath you can copy okay and then uh CSS you can copy absolute xath ABS xath means it's an Absol xath
14:20
Speaker A
relative xath you can directly copy from here so you can get it the EXP pass directly from this uh context menu options or you can directly inspect this element get the selector Hub this is a selector Hub the last class I have shown you this is selector Hub window right so this is
14:41
Speaker A
automatically generated if you look at here this is a x paath relative X paath and where is absor xath if you scroll down here you can see this is a absor x paath of the element okay you can directly
14:56
Speaker A
get them from the dev tools or by using selector Hub or or by using context menu so everywhere options are available you can directly get those xals but most of the times uh but sometimes it is okay you can capture the xath directly but sometimes we have to write our own xths I will
15:15
Speaker A
show you in which in which scenarios we have to write our own xath okay and always we cannot generate xath for every type of element but there are certain situations where we have to create our own xals okay let me I'll show you that options also but as of now this is a b absolute xath this
15:34
Speaker A
is a relative x paath two kinds of X paath we have one is called absor xath another one is relative xath okay so if you look at these two types of xths which xath we will prefer to use any guess
15:50
Speaker A
absolute xath Rel X there are two kinds of xath in our automation which xath we will prefer most of the times which xath will prefer okay so people are saying relative xath we will prefer to use most of the times okay so if I ask why relative xath is preferred when you compare these two
16:11
Speaker A
absolute and relative X paath which type of xath we will prefer is relative xath we will prefer most of the times we'll prefer relative xath okay yeah it is just a given one example I will explain this relative xath in detail okay so we have to learn about more about relative xath I'll
16:30
Speaker A
come to and explain the relative xath in detail but here I'm just showing types of xath stick to that so we will discuss again individually with more examples I will give you more examples just hold on for some time okay now why we will use relative expath why we will prefer relative
16:51
Speaker A
expath okay there are multiple reasons so many people will say one single answer that is absolute xath is very lengthy whereas relative xath is very smaller okay so if I use Absol X paaths in our Automation in many places your code looks ugly right it's not clean but if I use short xath like
17:18
Speaker A
this it's very simple and also code looks good very clean right but this is the answer most of the people will tell but the actual reason is sometimes Absol xath will may be smaller than relative xath suppose if the web page is very smaller and there are very few number of elements
17:37
Speaker A
times your absolute xath will be smaller than relative xath okay but what is the actual reason behind that why we should use relative xath this is the most popular inter question so how many types of xths absolute and relative xath and why which xath we will prefer relative xath we will
17:56
Speaker A
prefer why relative xath is preferred it is not because of length of the X path the main reason is suppose if you are using Absol X path let us say I have used absolute X path how it is going
18:12
Speaker A
to work how exactly absolute X path Works how it is going to locate an element for example let's say Absol path everything is a hierarchy right suppose this is element in this there is another element in this another element in this another element this other this is the actual element we
18:30
Speaker A
want to find out but from where the navigation is started from the root HTML so from here to here it should come again here here here and here it should find the element and here every tag is representing some element so if I look at here every tag is representing some element on the
18:49
Speaker A
web page every node is representing particular element in the web page so if you're using if you're using absur xath the problem is suppose if the developer changed something let's say he put some elements like this on the web page developer can introduce some new element somewhere in the
19:11
Speaker A
middle of something or developer can change the location of the web element suppose today logo is present here and tomorrow developer is got to change the logo position here and these changes keep happens in the real time in the real time projects on day-to-day basis and the UI changes
19:31
Speaker A
keeps happens so when the UI changes are happens or developer is added some element new element okay developer is removed some element existing element or developer has changed the position of the element these changes definitely will impact the hierarchy right suppose if this particular got
19:52
Speaker A
changed into some other position then what happens this observative xath connection is got connection is got Disturbed right so then what happens the navigation cannot reach to the El so this is the major problem for the absolute expath okay so why we should not prefer absolute expath because
20:13
Speaker A
developer keep doing some changes on the web page they can add a new elements or they can remove existing element they can do a reposition element because of these changes the hierarchy will keep impacted and our xath willo will be broken then we cannot locate our element these chances are very
20:34
Speaker A
very very uh H so because of those changes our X paath will impact that's the reason we don't prefer absolute X path okay but don't we have such problem in the relative x paath no why because in the relative X paath basically it will work based on the attribute of the element we specify some
20:56
Speaker A
attribute so SL SL SL will directly jump to the element whichever is having this attribute so it will not navigate throughout the hierarchy or throughout all the nodes so even though element is got added some new element is got added or existing element is got moved the attributes will
21:15
Speaker A
not change so this xath will always works so that is the reason relative xath is always referred it is not because of size of an xath it is because of changes on the web page the changes in the
21:30
Speaker A
web page definitely will impact absolute xath but that cannot be impacted relative X paath so that is the reason we always prefer to use relative X paath okay so very important question so I'm going to write here which X path will be preferred always we will prefer relative X paath okay now
22:01
Speaker A
what are the major differences when you compare absolute and relative xath what are the major differences this is also another most important interation during the interview so difference between X paath absolute and relative X paath so difference between absolute and relative X paath just now we understood one difference what
22:32
Speaker A
is it one difference we noticed so absolute xth will navigate each and every node till it reaches the actual element but relative X paath will directly jump to the element based on the attribute which we provided that's the one difference apart from this there are some
22:49
Speaker A
more differences are there so let me put it here most popular inter equation the first difference observed xath will always start with the single slash whereas relative xath will always start with the double slash remember this single slash will navigate only one level but double slash can
23:12
Speaker A
navigate multiple levels that's the reason in Absol X path when I use single slash it will go to the only Next Level only one level so under body there's a header from body to D we cannot directly jump why because we are using single slashes here and double slash can directly jump
23:31
Speaker A
to the element okay so Absol xath will start from single slash relative X path will start with the double slash this is a one important difference so single slash is always representing the root node that is HTML is a starting node on the HTML that's a root node absolute exper do not use attributes
23:53
Speaker A
if I look at here in absolute xath we haven't used any attributes of the element we used only be tag names it can be any tag name okay in between HTML root node to input tag whatever the elements are
24:06
Speaker A
there in between these two it is captured the tag names of all the elements and it is made a root or it is made a path that's called X paath right so absolute X path don't use any attributes whereas
24:21
Speaker A
relative X paath Works based on the attribute it will use set an attribute without attribute we cannot create a xath but there are another ways to do it if I don't use attribute but attribute must be there so in relative xath we have to use some attribute third difference Absol X path Travers
24:41
Speaker A
through each and every node till it finds the element so it will start from HTML it will go to each and every node and finally it reaches the input element and relative X path directly jump and find the element by using at Ute so these are the differences between absolute xath and relative
25:05
Speaker A
xath so during interview if anybody ask you you should explain all three differences so absolute xath start with single slash relative xath will start with the double slash absolute xath will use attri absolute xath don't use attributes whereas relative xath will use attributes absolute xath
25:26
Speaker A
will navigate throughout the Dom whereas relative X paath will directly jump and find the element in the do so these are the three differences okay so the performance wise is almost the same even it is Absol xath and relative xath performance wise there is no much difference okay but user
25:49
Speaker A
perspective there is a n number of chances are there in Absol xath definitely will will be broken because developer keeps doing some Chang on the web page because of that absor XO will always impacted so that's the reason absor XO is not preferred relative XO is preferred it is not
26:08
Speaker A
because of uh performance but slight difference is there but that is negligible per that is a negligible as a user we cannot see that much of difference when I use OBS relative X paath okay yes attributes in the sense we already know what what is attribute means we already discussed what
26:28
Speaker A
is an attribute what are the attributes every element is having certain attributes right see for example if I look at this input box so what are the attributes of the element type is one attribute name is an attribute placeholder is value class is an attribute these are all
26:47
Speaker A
attributes so xath can use any of these attributes okay xath can use any of the attribute so that's called attribute so absolute X path use attribute but sorry relative xath we use an attribute but absolute X paath we don't use any attributes we use only tags of the elements okay so these are
27:12
Speaker A
the differences between absolute X path and then relative X path now absolute X path is very very simple because just it is a hierarchy right and it can be generated as I said we can just right click here copy and copy full xot this is the option which is available or else you can directly use
27:35
Speaker A
selector Hub and copy Absol xath you can use this option or you can directly go to selector Hub tool and you can get it from the selector Hub here you can see Absol xath so you can get it from
27:49
Speaker A
here and if you press enter it will check whether this xath is able to locating element or not so these are the multiple ways we can capture the X path but Absol xath is straightforward means what it will directly navigating from the root node it will generate the xath so now you guys
28:07
Speaker A
can tell me I will write something here you guys can tell me the absolute xath how we can write absolute X paath okay let's say I have some node called a and uh then B and then C so inside this
28:24
Speaker A
a again I have a sub noes A1 A2 A3 inside the A3 I have a31 a32 a33 and inside the C also I have a C1 let's say these are all different nodes okay C3 in the C2 I have again C uh c21 in the c21
28:48
Speaker A
I have a C 21 1 okay let's say this is my HTML hierarchy or Dom hierarchy now I want to locate this particular element now tell me how to write absolute X path first we will start from the root
29:05
Speaker A
a slash inside they have a three nodes A1 A2 A3 so which node we have to go here A3 so a/ A3 SL inside the A3 again I have a three nodes a31 a32 a33 which element you want to find third element
29:24
Speaker A
so a 33 so this is our X path okay similarly I want to find c21 1 this is the element I want to find then how to write X path starting node is what a BC this is all comes under this
29:40
Speaker A
is starting node a BC a BC and again let's say this is there is a root node inside this uh we have a b and c okay now let me tell you root is a node from the root we have to to go to the C
30:00
Speaker A
inside the C we have a three node C1 C2 C3 which where we have to go C2 C2 slash inside the C2 we have a c21 c21 slash inside this we have a c21 again c21 one so this is how we can write a ab
30:17
Speaker A
x path from the Dom but it is very challenging if you have a hug or very complex web pages okay because you sometimes you may have 10 elements in between you may have 20 30 50 elements if the page complexity is increased obviously the ABS X path complexi also will be increased so
30:37
Speaker A
it is not recommended most of the times first of all but if you still want to write an exact absolute expert you can simply write like this by using hierarchy we can by based on the Dom we can clearly write our own exports okay this is simple so we don't need to discuss more about
30:53
Speaker A
absolute X paath because relative xath is most important than absolute X paath so now you got some idea about the X path right what is an Absol X paath basically absur X paath is the complete path we have to write by specifying the each and every node means each and every tag of
31:10
Speaker A
the element till we reaches the uh expected element that is an absolute X path so it is not big thing uh we can simply write a absolute X path now let us discuss more about relative X PA because that's the most important so let us discuss about relative X paath and which is also
31:32
Speaker A
called as a partial X paath so the relative X paath we can generate automatically and we can also create manually so two different ways we can create a relative export first approach we can do automatically okay automatically we can create second option is we can create manually so can
31:58
Speaker A
do we can create relative xath manual or we can say our own xath our own xath we can generate okay so automatically means we can use either Dev tools option okay Dev tools option or by using selector Hub selector Hub we can automatically generate by D tool selector we can automatically generate so
32:26
Speaker A
example let's go here and suppose if you want to create an xath for this logo element you can just right click and inspect and go to the script here right click copy here you can find copy full xath
32:45
Speaker A
copy xath so these two options are available from the dev tools and from the selector Hub if you want to use again just right click on the element directly go to selector Hub and here you can get these options copy relative x paath copy absolute xath you can see these two options okay
33:02
Speaker A
I'm just copying relative X paath for that logo element and it seems like this see it is used some attribute of the element okay and absolute xath if you want to capture absolute X path you can just right click and select our Hub and you can say uh here this is copy ABS X path so this will
33:26
Speaker A
give you Absol X paath okay so you can directly capture the xath automatically and if you want to use selector Hub what you can simply do it and inspect and open the selector Hub tool so here go to selector Hub and in the selector Hub this is our selector Hub so here you can directly get
33:54
Speaker A
the element so inspect this element as soon as we inspected here you can see this is a x paath relative X path and this is a if I scroll down you can see the absolute xath this is the
34:08
Speaker A
absolute xath and if you want to verify that xath is correctly working or not you can just double click here it will copy and press enter and this will find one element is matching same thing for absolute xath if I go to Absolute X path this is Absol X path of the element press enter and
34:26
Speaker A
it is finding the one element so it is very easy to generate xpaths it can be absolute or relative xath we can easily generate by using Dev tools and the selector okay but how we can generate manually manually I want to generate my own XO so then we have to follow a syntax so if you know the syntax
34:49
Speaker A
you can easily generate our own xox okay so let me tell you what is a syntax how to generate our own xath I'll give you multiple examples and syntax is first thing you need to start with the SL slash and you can put the tag name here tag name of the element in the bracket specify
35:11
Speaker A
the attribute and value of the element and we need to start with at theate symbol at theate here attribute name equal to attribute value you have to specify in single or double quotations so this is a syntax and other is suppose if you don't want to specify the tag name or if you don't know
35:32
Speaker A
the tag name you can also put simply star star representing a regular expression so this is a syntax so tag name is Tag name is not optional you should specify the tag name or at least you should specify the star if you don't know the tag name okay and attribute we can specify by
35:50
Speaker A
using at theate symbol here we use at theate at theate attribute name equal to value of the attribute and here also at theate attribute equal to value of the attribute so this is a syntax so if you know the syntax you can write XB for any element now let me show you how we can write
36:11
Speaker A
it just a moment okay now let us see how to write a relative xath for few elements let's go and check any element so in this particular page I want to write relative xath uh for some element let us say you
36:42
Speaker A
can take any element let's say I I will take this desktop image element okay so inspect this element and if you're inspecting this element so this is the HTML of this T this image right so how to write relative xath for this image what is the tag name here IMG is a tag name so say IMG
37:03
Speaker A
SL slash start with a SL slash IMG in the bracket and what are the attributes are available for this SRC is available but most of the times we don't use SRC because this is representing the target URL alt property title and class okay and let me take the title title equal to Macbook so let me
37:27
Speaker A
capture this this entire thing go back and here at theate that's it title whatever the attribute is available you can specify equal to the value of the attribute so this is the relative exp part very simple and you can try this directly whether it's working or not how we can verify you can go
37:46
Speaker A
to the selector Hub and you can just paste it over here then press enter now we can see this x paath is exactly matching with one element okay so we can simply write our relative xath if you know the syntax of the element so what is the syntax SL SL even if you put the star also this
38:06
Speaker A
will work suppose if you don't know the tag name you can simply put star so this will also find the one element so in the relative xath mainly it will works based on the attribute you must specify the attribute of the element along with the tag so if you know the syntax you can easily write
38:25
Speaker A
our own X paaths for the element and again there are different types of relative expath different ways we can write there are multiple things like we can specify multiple attributes we can use some built-in methods in this we can use different operators and R operators and chain EX parts
38:44
Speaker A
there are multiple flavors of relative expath are available so we will discuss them one by one so for everybody is clear got some idea about the xath what is an xath what are the types of xath how we can create an xath so here you can put single or double codes so if you put double codes
39:04
Speaker A
what you need to do is you need to use something called regular expression like this why because in Java we have to keep this entire xath in double quotations so double quotations inside the double quotations are not allowed so we should specify the regular expression by using slash and instead
39:22
Speaker A
of that you can simply put single quotations okay put single quotation that is also fine right so this is how we can create relative xath based on the syntax right so now we'll see different types of xpaths in relative xath only we have a different flavors we will see all of them one by
39:45
Speaker A
one by using example so now let's go to Eclipse directly and then create new package dat 24 inside this I'm creating a new class name it as a xath demo taking main method and set finish okay so now I'm going to show you how we can use an xath in our automation
40:16
Speaker A
with some examples first of all I want to launch my browser so for that I can say web driver driver equal to new Chrome driver I'm taking all right so import Chrome driver or you can say control shift o so that will import required packages and then we will
40:40
Speaker A
launch our application I can say driver dot get and here I will specify this open cut demo application this is the URL and how to maximize the page what's the command driver dot manage dot window dot driver. manage do window. maximize okay this will maximize the page now so let
41:12
Speaker A
us try to use xath the first example x paath with single here I'm showing you all of them are relative xath okay xath with single attribute xath with the single attribute how we can write suppose this is the input box I want to locate this is input box I want to locate just inspect
41:37
Speaker A
this element and there are multiple attributes are there so I want to create an xath for this element if I go back to the selector Hub you can write your own xath or else you can capture this xath and if you look at this this is a single attribute which we have used so how we can use
41:54
Speaker A
xath driver dot find element by dot xath so here we have to call X paath method X paath and specify the X paath like this and if you put double quotations what is the problem in this is you will see some error kind of thing so why we are getting this error because the double
42:15
Speaker A
quotations we already used but inside this again another double quotations we are using this is the problem so you can put the single quote inside this okay now this is the next path of the element and once you locate an element then I can pass some value in it let's say I'm
42:32
Speaker A
passing uh you can pass any anything so I can pass t-shirts like this okay this is having single attribute so the relative xath is having single attribute this is an example for single attribute we can also specify multiple
42:51
Speaker A
attributes but this we have used only one single attribute so let's execute and see okay it is started execution yes now we can see there is a value which is got as sent here input box is recognized fine so this is one single x paath x paath with a
43:17
Speaker A
single attribute but sometimes we can also specify multiple attributes if you want to specify multiple attribute you can specify so X path with multiple attributes suppose if you want to specify xath with multiple attributes how we can specify so this is for single
43:41
Speaker A
attribute no no it is executing very fast actually I'm using two monitors so that's the reason my browser is opening in my main monitor so I'm just dragging into secondary monitor so that's the reason still it is showing like that otherwise
44:05
Speaker A
it will maximize it okay so let me run this one more time it is going very faster so as soon as you executed my browser is open now you can see yeah it is maximized right so now how we can specify multiple attributes in the xar so to specify
44:26
Speaker A
the multiple attributes in the X path uh you can see like this okay I will also copy this uh single attribute X path with single attribute this is an example so now xath with multiple attributes so if you want to create an xath with multiple attributes we can still create so if you want
45:01
Speaker A
to do it you can say open cut application and then so for the same element I want to specify two attributes then what you can do is go here and go to selector Hub and here you can write
45:13
Speaker A
your own X paath so this is the element I want to write our own xath by specifying multiple attributes SL slash input is a tag name and the first attribute I'm using uh name name equal to search okay and second attribute if you want to specify use one more bracket okay and here you
45:38
Speaker A
can specify at the rate one more bracket at the rate and you can use any other attribute for this element so I have something called placeholder so that I can use at theate Place holder equal to and the values are case sensitive exactly you have to provide so you can see the value of the
46:01
Speaker A
name is such s is a lowercase character and the value of the placeholder is also search but the value is started with the uppercase character so when you press enter this is able to locate an element okay so this is a x path of the
46:18
Speaker A
element by specifying multiple attributes we can write our own xal so so which one you have not understood Vera can you tell me I can repeat so x paath with a single attribute xath with the multiple attributes if you want to specify multiple
46:40
Speaker A
attributes you can specify in another bracket so this is one attribute this is another attribute okay if you put multiple attributes it will be more stronger so like this also we can use x paath by specifying multiple attributes so how we can write it now Drive dot find
46:59
Speaker A
element by do X paath and here I can specify the X and the dot send Keys here I'm passing the value so this is how we can use x path with multiple attributes you can specify n number of attributes whatever is available for the T okay so this is one more example
47:29
Speaker A
so instead bracket bracket we can use and that is a different case I'll come to that they are operators end or they are operators so if you want to specify multiple attributes you can specify like this so this is one attribute and this is another
47:44
Speaker A
attribute for the same element okay so this is another flavor let's execute now run as Java application see my web page is maximized and then you can see the values got entered okay so xath with the single attribute xath with the multiple attributes now in the xath we
48:11
Speaker A
can also use operators we have something called end or operators X path with and R operators so if one value is correct if one value is wrong it cannot locate the element both attributes should be correct okay if you specify multiple attributes even one attribute
48:38
Speaker A
is not correct this cannot be located the element it will throw exception so whatever attributes are specify here all the attributes should be true all the attributes should be correct then only it will able to locate the element okay all right so now X path with end R operator there are two
49:00
Speaker A
operators we can use in X path end operator R operator most important listen carefully end R operator again in the end R operator we can specify multiple attributes okay for example look at the same input box so this is one attribute right at theate name equal to search here I'm
49:26
Speaker A
using something called end operator end end at the rate placeholder equal to search okay just observe so this is the EXP I have return and this will able to locate one element let's try to understand this in this x paath i specified multiple attributes single bracket but I specified
49:53
Speaker A
something called end operator here so at theate name equal to search and at theate placeholder is also suchar so how exactly this end operator works when I use this x paath when it will able to locate the element if both the attributes are correct then only this xath will able to locate
50:13
Speaker A
the element it is almost similar to this one okay but here we use additional end operator so how exactly end means end means first one and second one both attributes should be correct then this locator will identify the element okay suppose if I'm using R operator instead of end I say R
50:38
Speaker A
then then at least one attribute should be matched suppose this first attribute is correct but second one is not matching will it identify element or not yes still able to identify similarly the first attribute is not correct but the second one is correct correct in that case also it will
50:59
Speaker A
able to locate the element so end operator means both attributes should be true then only it will able to locate an element in the r operator at least one attribute should be true at least one attribute should be true then only it will able to locate the element if one of the attribute is
51:19
Speaker A
false all both uh if both attributes are false or both attributes are not matching with any element then finally it cannot locate a element now if I look at here I'm using end operator so one element is matching so for example uh I'm giving some incorrect value see Zero element
51:39
Speaker A
so even though the first attribute value is correct the second one is wrong that's the reason end operator cannot locate any element zero elements okay suppose instead of end I'm using r r now able to locate element why because at least one attribute should be matched then our
52:02
Speaker A
operator will able to locate a element end means what both attributes should be matched then only the xath will able to locate the element okay so you can put multiple multiple attributes not only two attributes you can put multiple attributes 3 4 five also you can put right this is about end
52:24
Speaker A
R operator end means what both both should be true then only it will able to locate element or means at least one should be true then only it will locate operator when it cannot locate if both attributes are not correct then it cannot locate the element okay so let's try to use in
52:43
Speaker A
our automation so that is up to you if you want to use multiple oper multiple attributes you can use that will make your xath very stronger okay so suppose uh if I use this one end operator that means you are expecting both attributes must be there you're expecting both attributes must
53:02
Speaker A
be there in that case you can use end suppose at least one uh at least one attribute is matching then you can find that element if you want to find element at least with one attribute you can specify both attributes and even one is okay or one is match then it can find that element in that
53:21
Speaker A
particular scenario we can use and or so multiple attributes make your xath more stronger instead of depending on one attribute you can also specify multiple attributes that will make your xath very stronger okay so if your element is having one single attribute right there is no question of
53:44
Speaker A
using second uh multiple attributes so you have to use only one single attribute especially suppose you have an Anor tag which is having only one attribute called HF attribute right so other other than this there is no other choice so in that case you can specify only one attribute
54:02
Speaker A
that is again based upon the availability okay if the attribute is having multiple attributes if the element is having multiple attributes provided in the Dom you can use it in xar if not provided one single attribute is available then you can use it okay that's still F even
54:17
Speaker A
though attribute is not available still you can write an X path I will come to that part suppose I don't have any attributes for the element still can we write an X path yes we can still write an xath for the element even though the attributes are not available I will
54:32
Speaker A
come to that how we can write okay so xath axis no here I'm not discussing any xath axis so don't discuss that okay we will discuss xath axis which is very Advanced concept we will cover in the next session so this I'm just talking about some basic type of xath today I'm not
54:50
Speaker A
going to deeper aspects okay just stick to that right so end or op operators let's go back and let us see how we can use them so the next One X path with end operator X path with end operator
55:11
Speaker A
yeah so we can write driver. findind element by. xath in the double quotations we can specify this XPath right so here end operator we have to use and Dot send keys of we can pass the value okay so this is how uh we can use multiple attributes with end operator okay
55:52
Speaker A
execute okay now maximized now you can see the values got entered same thing we can try with r operator so if I just look at here R operator so instead of end I'm saying r r means at least one should be true even secondary is not correct still it will work fine so what
56:19
Speaker A
I will do is I make placeholder is some XY Z it is incorrect but still it will able to locate l okay now we can see the values got entered so this is a r operator x paath with end R operators
56:45
Speaker A
okay got the difference everyone so difference between end or end means both attributes should be true should be matched then only it will able to locate element R means at least one attribute should be true then only it will able to locate the element that is a major difference between and
57:02
Speaker A
R yes if the developer keeps changing the attributes and all we will try to handle them in different way that is a different concept again so how we can handle the elements those attributes are dynamically changing that's a different concept I will come to that part so these are
57:23
Speaker A
X paaths which we have currently using is for static attrib rutes if the attributes are not changing they are constant then you can go with this attributes but initial stages the developer do more more number of changes but once your build is stable right once you get two three
57:41
Speaker A
four five builds in every cycle you'll get a new builds slowly the stability come into picture so once the build is stable then your xath and attributes everything will become stable so you don't need to change every time but in initial stages the attributes are El keeps
57:57
Speaker A
changing in those cases static stuff will not work properly okay how to handle the dynamic elements I will come to that part that's the next topic okay this is xath for and or I will cover each and everything guys you don't need to ask before itself first let us listen first okay once you
58:14
Speaker A
listen everything once I finish the session then if you have something to discuss you can ask the question and don't go beyond this topic so if you already know something keep fight sometime okay don't ask ask anything if you already know something and you can ask the question at the end
58:30
Speaker A
of the session because otherwise unnecessarily other people will be confused I'll go step by step each and everything I will cover okay now and R operator we discussed with multiple attributes so there is another flavor suppose sometimes we can also write an xath with inner text what is
58:52
Speaker A
an inner text suppose uh if you locate any element inner text inner text will be there for few types of elements so for example let us say let's take any link suppose here desktops is there so let me inspect this element okay so basically what is what this desktop is it is a link right
59:14
Speaker A
anchor tag which is having HF attribute and class attribute some attributes are there you can use this attribute in the xath but I don't want to use any attributes of the X paath any attributes of the elements still I want to write an xath for this link how we can write it so we
59:31
Speaker A
need to capture the inner text so this is called inner text this is called as a inner text so by using this inner text we can find the element we can find the element by using inner text okay so
59:46
Speaker A
let me show you how we can find this inner text capture this inner text and we can write X paath like this x paath with inner text so inner text means what here we have to use something called a
60:00
Speaker A
method x paath with text method there is a method is there which is called text so I will write the syntax a little bit change when I use methods SL slash tag name okay what is the tag name
60:14
Speaker A
of the desktop element anchor tag right so a in this I'm not going to provide any attribute I'm not going to provide any attribute simply what you can do is I can use one method called text equal to what is the value of the text here is MacBook this is the inner text desktop whatever
60:34
Speaker A
you want you can find out desktops I'm taking so you can put desktop so this is also next PATH so have we used any attribute in this x path no we haven't used any attribute of the element and whatever we have used here this is what inner text okay the text is a method if the text value
60:54
Speaker A
of the a is equal to desktops then it will go and match with that element then it will find that element okay similarly if I want to locate this MacBook inspect this MacBook link and this is also inner text and tag name is a so without using attribute also we can sometimes we can
61:17
Speaker A
write an X path so here we can say text bracket equal to you can specify the value like this so this is also X paath so by using text method we can find the element and when this is Possible
61:32
Speaker A
only if you have a inner text then only it is possible but if you look at the input box here do we have any inner text for this input box do we have any inner text for this input box yes or
61:47
Speaker A
no yes most of the times link text is almost link text is also we considered as inner text but sometimes inner text doesn't have a link I will show you that okay so for example if I look at here input box do we have inner text for this input box do we have an inner text for
62:12
Speaker A
this input box no we do not have a inner text for that so we cannot use text method for this element okay suppose I'm uh I'm locating this one featured feature text do we have inner text for this H3 tag name do we have attribute or do we have inner text for this element yes do we have
62:38
Speaker A
attributes for this element attributes no okay so this is this a link is this a link featured is this a link no this is not a link this is not having any any attribute it is just a simple and
62:58
Speaker A
inner text okay so here MacBook is inner text or not yes this is also link this is also link text and also part also inner text but in this in this case featured this is not a link text this is not
63:17
Speaker A
a link text but it is a inner text you got my point it is an inner text so you understood the difference between this one inner text and Link text the link L text can be in inner text but
63:29
Speaker A
all inner text cannot be a links that's the point I want to convey okay now let me write something like h a anchor tag a HF equal to okay some htdp is something XY z.com something is there okay and here I'm say click me and closing Anor now tell me is this a link text or not click me
63:56
Speaker A
is a link text or not click me is a link text or not yes it's a link text yes and is it inner text or not is it an inner text or not yes it is also inner text so this is the inner text and also it
64:15
Speaker A
is a link text suppose if I write like this div I'm writing a div no attributes welome come and again D tag is got closed now in this is it a link text or not is it a link text no do we have inet
64:37
Speaker A
text here yes you understood now what is the difference between inner text and Link text so the link text can be an inner text but all inner text may not be a link text until unless you have this HF attribute you cannot say that is a link okay so if you have this kind of inner text and
65:01
Speaker A
if you do not have any attributes so by using inner text we can still locate an element it is still locate an element without depending on any attributes still we can locate an element by using inner text how it is possible by writing a text method okay so let me show you so here x
65:24
Speaker A
path with text method so how we can write driver. find element by. xath in the double quotations we have to specify SL slash if you specify the T tag name you can specify or you can put star no problem and in the bracket text method equal to the value of the text is what I say MacBook
65:58
Speaker A
okay so this will able to find the MacBook element then I will perform the click action on this click action on this MacBook I want to perform the click action like this because that is a link type of element we can perform the click action okay let's execute now
66:15
Speaker A
and first of all let us check this is working in the Dow tools go back to the page okay now go to selector Hub so I'm just pasting the xath here press enter yes now it is matching with
66:42
Speaker A
one element so xath with text method so now we can directly use it here then run yes now it is successfully clicked so it is going to the next page right so x paath with text method so here we consider inner text we consider the inner text we
67:10
Speaker A
are not using any attributes in this we are not using any attributes so by using inner text we can still find the element in the xar okay let me show you one more in the same page I want to capture this text featured okay I want to print that in my
67:35
Speaker A
console window that is not error actually that's application related error so when I click on the MacBook it is if you do manually it is working fine but when you try to automate because we are doing through automation right it is asking for some kind of authentication so that's a
67:52
Speaker A
problem with the application not our code okay now this link is fine but what about this one featured featured this is just a normal label it is not a link right so how to capture this inspect and uh inner text right this is inner text featured is inner text so
68:17
Speaker A
let us try to capture it so can we do any operation on that label can we do any type of action on this if it is a link we can click on it but it is not a link nothing
68:30
Speaker A
it is just a label so what we can do we can capture this value text value we can capture and we can print in the console window right so let's write an xath for this okay sl/ H3 is a tag in the bracket I say text equal to
68:51
Speaker A
featured that's it now now one element is matching now we can use this yeah we can check display status we can also capture the text value so you can say this is the X paath now let me write driver. find element by. xath in the double quotes we put this okay now if
69:21
Speaker A
you want to just verify this display status you can write is dis displayed is displayed so this will return return true or false so display status and what is the type of this variable is Boolean type of variable and then you can print display status true or
69:42
Speaker A
false okay suppose if you want to capture the text value of this element so then what you can do dot there is a method called get text do get text so that will capture the text value of the element that I can store in a string variable okay or string value that I can
70:09
Speaker A
print right so I can check the text value uh that label is displayed or not if it is displayed I can also capture the text value of the element yes you can compare this value with your expected value by
70:29
Speaker A
just writing one if condition you can check this value is correct or not okay let me run it okay now we can see we got output as a true and also featured so this is how we can capture this
70:55
Speaker A
sh selector is not related to selenium okay this is related to some other tools it is no so the selector have is not only for selenium selectors okay it is also supported some other tools like test Trier play right Cypress locators so all types of tools are supported as of multiple
71:15
Speaker A
tools are supported so this sh selector is not related to uh cenum selector okay right so now we have understood how we can use text method along with the X path everybody's clear so far in this x paath with text method how we can use and when we have to use if you have inner text of the element
71:42
Speaker A
then you can use it and if without using any attributes we can still write an X path by using text all right so there is another method just like a text we have another method called contains contains x paath with contains method
72:04
Speaker A
contains okay so let us see how we can use this uh contains method very simple let's say I want to locate this input box called search inspect this element and there are some attributes are available for this element so name placeholder and so many things right so what I can do is
72:32
Speaker A
I can write an expo for this like this SL slash input here I'm not going to specify any attribute I'm not going to specify any attribute but what I can do is I can say one method called contains
72:47
Speaker A
method is available contains here we have to pass two parameters contain method will expect two parameters so first parameter is which attribute I'm using placeholder and comma the second parameter is the value of the placeholder so what is exact value of the placeholder is
73:10
Speaker A
search search is an exact value but here I will just specify only s e a c that's it and then close this single quotation so this will able to locate the element so zero element as of now why it is zero element input this tag name is not correct yeah one element so if I look at this how
73:36
Speaker A
exactly we have written this what is contains method will do see when I use contains method specify the name of the attribute and specify the value of the attribute so when I use contains method I no need to provide the complete value of the attribute I can just specify some partial
73:55
Speaker A
value of the attribute so in the strings also we have a contains method right yes exactly it is a substring in the strings we discussed about contains method what this will do it will return return the subsection or substring of the main string similarly the contain method will verify
74:14
Speaker A
this wherever the placeholder attribute is having this particular string that will go and match with that element so contains me we can verify the partial value of the attribute we can verify the partial value of the attribute so whichever attribute we specify here that attribute contains
74:34
Speaker A
this one and that will go and match with the element so this is all about contains method you can use any attribute here but you can you don't need to specify the complete value you can just specify some partial value that's called contains method okay let's go and use it
74:50
Speaker A
here contains X path with contains method so driver dot find element by. xath dot this is uh input box so I'm using send keys right so if I look at here input is a tag contains is a method placeholder is an
75:31
Speaker A
attribute and this is a value of the attribute is not complete value some partial value I'm provided so this will able to locate element so this is the contains method contains means it will verify the partial text value of the element so it is able to locate it same way we
75:52
Speaker A
have another method called start with X paath it is almost similar to cont but slight difference is there x paath with start with function start with start with so in this also we can write same thing so instead of contains I can say start with and again placeholder and
76:17
Speaker A
the attribute value starting with sea right so you can keep that no problem so instead of contains I can also use start with it is another function or another method available in xath okay now it is got enter the value let me run one more time there is some exception
76:42
Speaker A
yeah okay so this time we got some exception so we'll see first of all this particular X paath is correct or not let's copy this okay see there is a syntax error so red color Mark is representing a syntax error it
77:17
Speaker A
is clearly saying selector Hub also can cross check whether it's valid syntax or not so it is saying invalid syntax now let us check why it is invalid syntax SL slash input it is I think it is a start starts plural okay it is not start it is starts starts with placeholder c yeah now it is
77:40
Speaker A
matching so instead of start you can say starts okay so now it is able to locate element so this is how we can use start now what is the difference between contains and start with can anyone guess what is the difference between contains and start
78:15
Speaker A
withd difference between contains and start with so in contain method this value which we provided can be anywhere in the string okay actual string is what see in both the cases what is an actual string search right this is an actual string that's what expected string so in the contain
78:47
Speaker A
method we just say only s EA that is a part of the main string so it is matching with this but in start with also the search is start with sea so this is also matching so the major difference is
79:01
Speaker A
cont method will check the string anywhere it can be beginning or it can be middle or it can be end wherever it is comes it still matches but start with method will not match like that okay start with method will we have to this particular string has to be beginning of the string so
79:19
Speaker A
then only it will able to match okay that is a major difference between contains and start and one more important thing so this contains and start with functions we can use whenever you want to locate Dynamic web elements whenever you want to locate Dynamic web elements you can use start
79:41
Speaker A
with and contains functions so let me tell you suppose your attributes are keep changing the values of attributes are keep changing then how we can locate those Elements by using exp and most important entry questions how we were able to handle the dynamic elements using XA so here
79:59
Speaker A
we use something called contains or start with functions so let me show you an example how we can handle the dynamic elements using start withd and contains finding very very important scenario handling Dynamic attributes Dynamic attributes in the sense what the attribute values are keeps
80:22
Speaker A
changing at the run time so suppose if you are using some ID in the xath same ID will not be there in the runtime so then xath will definitely won't work so in that case how we can handle this
80:34
Speaker A
so let me explain this with some examples so you guys can also tell me the answers for that let me take let us assume I have some button on my application my button name is called start I have a start button okay so when you click on on this button immediately it is turn into stop
80:59
Speaker A
so most of the times you can notice this kind of behavior for stop watches when you click on start the stopwatch will start and rotating whenever you click on the stop it will stop the same button is acting as a start and stop so once you click on it once you click on it
81:14
Speaker A
it will turn into stop same button there are no multiple buttons there is single button and it is changing start to stop again stop to start okay when the button is in start State let us say the ID value of the button is start okay when button is turned into stop then ID value
81:37
Speaker A
becomes a stop this is my scenario so when the button is in start stage the ID value start when button is in stop state ID value changing stop so now I want to write one single X path and
81:52
Speaker A
through that X path I'm able to locate element in whichever state it is having EI either it can be start or it can be stop in both the states I want to locate the element by rating a xath
82:05
Speaker A
then how can we write xath for this there are multiple ways are there now just tell me one by one Give Me One X path start and stop same button turn into start and stop again start and stop ID is start and stop stop so sometimes ID is a stop sometimes ID is
82:27
Speaker A
a stop then I want to write one single X path through which I should able to locate element for example if I write an xath like this whether it's correct or not you tell me SL slash I don't know tag name I can say star here I'm using something
82:42
Speaker A
called uh at the rate ID equal to start so this is the exper I have written so will it work first of all is this x paath correct the way of writing syntax the syntax the way of writing xath is correct yes xath is correct but it can locate element only one time that
83:11
Speaker A
means if the button is in start State then only it can identify but once it is turning to stop it cannot identify right it cannot identify same thing if you put stop here then it can identify only if the button is in stop state but once it is into start State this xath cannot work
83:33
Speaker A
so by specifying the ID directly we cannot locate these elements only in one state we can locate but another state the xath will will not work so we need to write an xath let us use R operator and
83:49
Speaker A
see there are multiple ways one approach is our operator let's see SL SL star at theate ID equal to start or at theate ID equal to stop can we write like this will it able to handle in both the
84:05
Speaker A
situations yes why because we use R operator can we use end operator here end no end means both the property should be true but at the time ID cannot be start and stop right so we should use only R so in case of start the first attribute will match in case of stop the second
84:29
Speaker A
attribute will match at least one should match so this x paath will able to handle this kind of a situation this is one approach this is correct now another approach by using contains also we can do it by using contains also we can do it how we can do it contains contains specify the atate
84:52
Speaker A
ID attribute and we need to identify one common thing in both IDs what is a commonly there in both IDs what is a common value which we have in both IDs first two characters are common right St is common so that you can specify here s okay so then this exper also can identify element in both
85:17
Speaker A
the states because in even if ID is start still it is working ID is stop also it is working perfectly fine now another way by using start with function you can say start starts with at theate starts with at theate ID here I can say again STD so will it work yes so these are the three different
85:48
Speaker A
because every ID is starting with st only right so this also works so we can also handle the Dy attributes by using cond and start with functions in multiple ways we can handle so these X paath we cannot generate we have to write manually okay these type of xath we have to create our
86:09
Speaker A
own okay so very very important so contains a text yeah that is also same contains a text contains that also you can write so another way you can do like this SL slash star okay and you can say in the bracket contains in the contains the two parameters it will take and
86:35
Speaker A
the text parameter comma here you can put s so contains text text is nothing but what the text should contains a SD so text method will capture the text value of the element and that contains St so this we cannot use for this element way because this will work only for
87:01
Speaker A
inner text okay the text method will work only for inner text and links for buttons we don't see any inner text most of the times okay so that's the reason if there is inner text then it will fine or else it will not work so these three are appropriate to handle this scenario
87:22
Speaker A
now I will give you some more examples you get can tell me when to use contains when to use start with you guys can tell me suppose I have a name attribute for the element the name attribute is keeps changing so initially the name attribute is what XY Z 0 01 XY Z 0 01
87:43
Speaker A
that is initial stage and after that the same name attribute is got change like this XY Z 02 XY J 03 XY J 04 or again XY Z 0 01 and XY Z 0 02 like this it keeps changing 1 2 3 4 again 1
88:04
Speaker A
2 again 2 one like that dynamically changing then how can you write an xath for this so tell me one exp yes contains and start with functions works if there is something commonly there but definitely there is some common thing will be there whenever you kind some Dynamic
88:28
Speaker A
kind of a behavior yeah I'll show you more example now tell me if the name is changing like this how can we handle this attribute by writing an X can we use contains here contains contains at theate name contains at theate name what is
88:54
Speaker A
the commonly there in all the names XY Z is a common so that you can specify XY Z is a common so this xath is valid can we use start with start with starts with at theate name comma X wet so we can use start with only right because
89:21
Speaker A
all the names are start with XY Z perfectly fine so change we can use start with also we can use both are valid in this context okay now 0 0 is also common right so can we write like this 0 0 also common in all the scenarios contains a name 0 0 can we write like this yes
89:43
Speaker A
that is also valid but in start with we cannot write 0 0 why because 0 0 is something in the middle of the string so it should have only XY Z fine so now another scenario now let's say my name attribute is like this name value is initially it is 0 0 1 XY Z 0 02 XY Z 0 03 XY Z
90:13
Speaker A
0 04 XY Z again 0 0 1 XY Z okay how to write an X path can we use contains here contains contains yes contains at theate name equal at theate name sorry contains is a method Open Bracket atate name is what can put XY Z because XY Z can
90:44
Speaker A
be anywhere in the string so we can put no problem can we use starts with starts with no why because the starting values keeps changing right 0 0 is a common you can you start with with 0 0 that is fine starts with name 0 0 because every name is starting
91:10
Speaker A
with 0 0 you can still use but 0 0 1 0 02 you cannot specify because the values are keeps changing okay so contain method you cannot use start with method you cannot use you can use with one zer suppose if 0 0 not available let's say 0 0 is not
91:28
Speaker A
there now can we use start with can we use start with no start with we cannot use okay then I'll show you one more scenario in this case start with we cannot use only contains uh will work now let me take another example name equal to 101 XY J 201 1 XY J 31
92:08
Speaker A
XY J 41 XY Z yeah now tell me can we use contains here contains contains we can use because XY Z is a Comm can we use start with start with we cannot use only contains SL slash star contains and here at theate name simply you can say XY Z okay so now you
92:52
Speaker A
understood what is the difference between start with and contains method so these are the two methods through which we can identify the elements their attributes are dynamically changing okay now let me use it in your example we already used okay
93:13
Speaker A
fine so now let us see uh another type of X path called chainex path chainex path yeah 01 also use the contains method 01 is common right yes contains name is 01 01 is common all are valid so now another type of X path is called chained X path so what is the chain X
93:45
Speaker A
path and when we need to go with the chain X path sometimes uh if you're not finding any attributes of the element and even you you are not having any inner text okay you don't have any inner text and you don't have any attributes for the element in that case how we can write an xath
94:04
Speaker A
for the element okay for example let's say here I'm taking this logo element if I inspect this element you can find IMG tag and there are some attributes okay so let us assume you don't have any attributes for this IMG you assume you do not have any attributes for this element then how we
94:30
Speaker A
can locate element you don't have any attributes you don't have any inner text so then how we can locate this element so if you don't find any attributes for the element you need to just go to the parent element okay what is the parent of this IMG what is the parent of this IMG tag what's
94:51
Speaker A
the parent y a is an Ang and let us assume a is also not having any attributes then what we should do again we need to go to another parent what is the parent of EA div div so the div is having some
95:07
Speaker A
attribute right so we will start writing xath from div tag div ID equal to logo can we write xath for this yes so how to write SL slash D atate ID equal to what is an ID value here is logo so I can say
95:25
Speaker A
logo this is an xath for the D tag but this xath is not direct xath for the image but my target is what image I want to look at but image doesn't have any attributes then I went to parent even
95:41
Speaker A
parent also not having any then again I go to another parent so I started from here so I have written xath for this logo then what is the child item for this div tag a is a child item and for
95:52
Speaker A
a IMG is a child item so we can say slash a/ IMG now in this xath what is the target element Target element is what image IMG is a target element but did we write an direct X path for IMG direct xath
96:12
Speaker A
for IMG no we have written xath for the parent from there we have navigator to child from there again navigated to the another child if I look at this it seems like the combination of relative and absolute xath right so till here this is a relative X paath from here it is a absolute X
96:35
Speaker A
path why because we are saying slash slash so the combination is called chained X paath this is a combination of relative and absolute X path which is called chained X path chained means what if you do not find any attributes of the element then we will check the parent element is having
96:54
Speaker A
some attribute so from the parent we will try to locate the child element so it is just like this let's say I have some element here this having another element this having another element now I want to locate this element no attributes then I will go to parent let's say this is a b c and
97:13
Speaker A
d go to parent elements if the parent elements having attribute then you can directly write xath for this and slash you can specify the tag name of the CH element even this is also not having having any attribute then go to another parent so here you can find some attribute ID equal to some
97:29
Speaker A
XY Z then you can write xath for this B like this slash at the right ID equal to some XY Z and from there you need to navigate two levels slash c/ D like this you can write this is called chain
97:45
Speaker A
expath this is called chain xal sometimes if you could not find any attributes of the elements you can try writing the path for the parent element from the parent element you can navigate to The Chain elements this is called chained X paath okay so now can we use this element use this
98:06
Speaker A
x paath yes let me check it in the selector Hub go to selector Hub and here I'm keeping this yes one element is matching so this is a EXA element so I can say this is chain X path so driver dot find element by. XPath and what we can do on
98:41
Speaker A
this image what kind of actions we can do on the image display status we can verify right so dot is displayed so we can take a Boolean variable image status so driver. find element TT path is displayed so why is giving an
99:24
Speaker A
error this will return a Boolean value I think this variable have used anywhere somewhere okay yeah so now let us print status so this will verify the status of the image element so chain expath means if the attributes are not available for the particular element we
100:07
Speaker A
can write we can take the attributes of the parent element and write an X path from there we can navigate to the CH element so that is all about chain X path okay so everything is clear so far so these are the basic understanding what is an
100:26
Speaker A
X path okay and how many types of X path which X path will be preferred and x paath with single attribute and multiple attributes x paath with end or operators x paath with text function xath with contains function xath uh chain X paath okay and how to handle the dynamic elements
100:48
Speaker A
using start with and contains function there are n number of functions are there but more widely we use start with and contains functions okay so apart from this there are some more options are there we can say expath access like following preceding parent child sibling
101:07
Speaker A
preceding sibling following sibling there are multiple options we can use along with the xath so in the next class we will discuss about the xath so what is a main difference between xath and CSS okay so before that let me answer this question what is the difference between contains
101:28
Speaker A
and can you write your complete question I could not understand this so difference between yeah can you write the statement okay so contains a text so this is a different actually so this this will work only for inner text okay if suppose inner text we are checking suppose
102:00
Speaker A
inner text contains something or not if you want to check you can write text and here you not specify anything nothing right so text contains nothing this will go and match so contains a text nothing so you can write another way okay it is not matching with these things okay
102:19
Speaker A
these scenarios are different but this works only if you have this is right syntactically this is correct actually this syntactically it is correct so when this will match if you have any inner text okay suppose I have something like this anchor tag or suppose I have some D tag like
102:40
Speaker A
this slash div do we have inner text in this inner text no okay if you write an xath like this contains SL slash do contains so here text is what empty there is no inner text so here I'm
102:59
Speaker A
not putting anything just simple put single braces empty single braces right so now this will match exactly with with this this element this du tag because there is no inner text here okay so text value is empty text is representing what empty no no value so this will exactly match with this
103:25
Speaker A
okay so another one is this one SL slash contains dot so dot is also representing a text so this is another way of representing the text instead of text method you can simply use dot also so dot operator also you can use instead of text so both are same both
103:49
Speaker A
are same okay these both are same instead of text method instead of text you can put dot also so okay the question is we usually use an attribute that is not repeative how come we have a list of values this is I'm just taking for an example see I'm not saying one attribute is
104:15
Speaker A
having multiple values no what I'm saying if the name attribut is keeps changing okay initially this is a attribute value name values one1 XY and after some time on the same name attribute is got changed 20 01 XY again same name attribut is got change something else so sometimes
104:34
Speaker A
if you refresh the pages the attributes are keep changing so that's where the web pages normally designed most of the times very if you look at the complex web pages the attribute values will not be same every time so I'm not saying all the values at a time okay so initially name values
104:52
Speaker A
this one let's say after refreshing the page name values got changing like this and after refreshing again another value are getting these are the different values are changing for the same attribute in that context I have taken this example but one attribute
105:08
Speaker A
cannot have multiple values one attribute is having only one value at a time okay remember that right so practice this example for today's session and I will upload one presentation today you guess can go through the presentation and whatever locators so far we have understood all
105:28
Speaker A
the locators are specified here this presentation I will show types of locators and different types of locators with an example all the screenshots example I have taken everything so this is example for ID so name attribute link text and partial link text class name tag name and also
105:48
Speaker A
CSS selectors whatever so far we covered CSS selector how we can use tag and class tag and attribute tag class and attribute the next PATH so we have discussed all these Concepts what is next PATH what is Dom document object model so you can see this is HTML this is the Dom view
106:08
Speaker A
of the HTML this is actual web page so then absolute xath what is an absolute xath what is relative xath syntax of relative xath xath with r operator xath with end operator xath with contains method xath path with starts with method xath with the text method okay so I'll
106:27
Speaker A
share this presentation today you can go through each and every slide and try to understand all the examples in tomorrow session I will continue xath xath with access yeah so now the difference between CSS and expa what is a major difference very popular interview question let us say your
106:47
Speaker A
Dom is like this this is your Dom structure Okay so if when I compare with cs and X paath CSS will go to only top down approach so CSS will go from top to down only one directional X paath
107:06
Speaker A
will multi-directional suppose if there is an element here you can get this element from by navigating from the top or we can get this element by navigating from the bottom also in all the directions we can get that element xath will Traverse throughout all the direction
107:23
Speaker A
of the Dom whereas CSS will Traverse Only One Direction so that's a major difference but how xath is Direct in all the directions today we have seen only one direction from the root node we have navigated each and every node and find that element from root node and this is again
107:41
Speaker A
top down approach but how come the Rex paath will navigate all the directions by using x paath axis concept so in the tomorrow session I will discuss about this how we can TR davers throughout the Dom in all the directions using xath axis so this concept we will discuss tomorrow session so
108:01
Speaker A
there a major difference CSS will navigate only in One Direction whereas xath can direct in all the directions we can move in all the directions chain X path goes from bottom to top no no no top to bottom so whatever chain X paath today we discuss that will go to because we identify parent
108:21
Speaker A
child and child right so first we have written xath for the parent from the parent to we go to child element so this is top down only so this is a top down approach so down to top we can do
108:33
Speaker A
only by using xath axis in the next class I will discuss about this okay in all the direction like parent to child child to parent child to child okay and parent to parent so like child parent and grandparent so child to grandparent also we can directly go similarly from the grandparent
108:53
Speaker A
to child also we can directly jump so all levels of navigations are possible in expar but CSS only top down approach is possible so exper axis I will discuss in the tomorrow session in detail okay so I'll stop the session today for here and tomorrow we will
109:15
Speaker A
continue
Topics:SeleniumJavaXPathXPath functionsXPath typesabsolute XPathrelative XPathDOMlocatorsautomation testing

Frequently Asked Questions

What is XPath in Selenium?

XPath is a locator strategy derived from XML that represents the address of an element on a web page, used to locate elements in Selenium automation.

What is the difference between absolute and relative XPath?

Absolute XPath starts from the root HTML node and navigates through every node to the target element, while relative XPath provides a shorter, partial path to the element.

Why does XPath sometimes fail to locate an element?

XPath relies on the DOM being fully loaded; if the DOM is not completely loaded at runtime, XPath cannot locate the element, causing errors.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →