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...