What will you learn in this lecture?
In the previous video, we learned how to use CSS to identify an element in selenium of this Selenium webdriver elements identification course. In this video, we’ll have to learn how to use absolute XPath to identify an element.
How to Use Absolute XPath to Identify an Element
Alright, my friends so let’s talk about the next identifier that we’re going to use to locate our web element and that’s called XPath.
XPath is a very phenomenal way that we’re going to locate our element because it helps us to locate the elements even when there are no unique properties.
I mean there are almost always unique properties but a lot of times using something simple like a link text or an ID or a name is not possible and so that’s when we use XPath to dig down to the element exactly that we want.
The standard definition for XPath is that it’s a standard navigation tool for XML however XML and HTML are very similar for those of you that know what XML looks like and you guys will just learned what HTML looks like and basically XML looks exactly the same except for XML was developed to convey data and HTML was developed to display data.
So kind of a different purpose of each one but they’re both structurally very similar in fact almost the same minus a few key differences.
So if you look at a standard HTML, that’s what XML looks like but anyways because of that, those similar structures we can navigate HTML using XPath.
So let me show you guys some examples.
Let’s pull up our friendly qtptutorial.net/automation-practice-page and then I’m going to come over here grab this highlighting tool and I can hover over this button and rather than using CSS, here I’m going to select XPath.
We highlight again and you guys can see here that this is the XPath that was provided to me by the tool when I highlighted the sum and this is the XPath that was given.
And now this XPath you can see it’s pretty long, right so what it does is it digs down into each of these sections until we get to the right place. So for example it starts of this ID post so that comes up all the way up to here right so then it takes the next child and comes in here and then in there and then inside of here and see how it keeps getting the area keeps getting smaller and then inside of that area is something else and then inside of that area is something else and then inside of this area is a button and inside of that is finally our click me link that we want to click upon.
So this is what’s called a relative XPath but it’s a very long relative XPath an absolute XPath would actually start at the very top and then keep drilling down this one skips a few divs it keeps a lot of dibs and it actually starts at this post ID but an absolute XPath would start all the way here and then keep coming down until it gets all the way down to that element right there.
So absolute XPath is obviously the easiest way to identify our element the problem is that it’s very long and that any change in the structure will break the locator.
So for example if you look at these two buttons you highlight this button and then take this Ctrl A, Ctrl C to copy it over into my notepad that over here for you guys and then so that’s this button right button number one let me put button number two.
Now check out these two buttons compared to each other, you guys see how they’re almost exactly the same the only difference is that this is form 2 and this is form 1, their location is different in the website because one is above another.
But what if I was to move this button over here on the side or move this Xpath to button on top or move it way on top or even get rid of this one?
If that were to happen this would no longer be true because right if you imagine the button that we want but now this button got moved down here.
This would be its new address you can think of this as like the home address right if we want to go to this house the family in that house has moved to a different place when you come here you’re not going to find the family that you want because you have their exact address instead what you need is a relative way to figure out where the family lives and then go to that location.
Right in that something a good analogy there’s if you had the family’s phone number that you wanted to visit and you say hey what’s your address and then they would tell you whether it’s this one or this one.
It wouldn’t matter.
But that’s where relative XPath come in and help us make our jobs much easier rather than using absolutes and absolutes, like I said, are very easy but they are bad and dangerous and when elements get shuffled around which they always do unless you’re working on a really bad application that never changes you should use relative XPath.
Okay so don’t use absolutes they’re just good to know but we’re going to learn how to use relative expats extremely well.
[Tweet “Learn everything about elements identification with Selenium Webdriver.”]
Next Tutorial
In the next tutorial, we will cover how to use relative XPath to identify an element.
What will you learn in this course?
Are you struggling with working with HTML using Selenium WebDriver? Do you know how to easily identify an element using Selenium WebDrriver? Do you know how to manipulate that web element? Do you know how to perform a drag n’ drop on an element? If not, then these are just a few of the questions that will be answered in this course.
This course is a complete guide on working with web elements in Selenium WebDriver! Once you are finished with this course, you will know how to work with any web elements, any time, on any web application.
“Relevant content that I will be able to apply to my automation tests *immediately*, a VERY effective presentation approach (evolving the same example and comparing/contrasting), and an excellent teacher / presenter. Thank you so much for your time and sharing your knowledge Nikolay!.”
-Krissy
In this course from Ultimate QA, you will learn:
- Basics of HTML
- All the different locator strategies for Selenium WebDriver
- How to identify web elements using Selenium WebDriver
- Master XPath
- Navigation with Selenium WebDriver
- Web element manipulation
- Web element interrogation
- Mouse and keyboard actions with Selenium WebDriver
- Performing actions such as drag n’ drop, drawing, hovering
- Implicit and Explicit waits
- How to properly handle element identification so that your tests are not flaky
- Expected Conditions in Selenium WebDriver
Take This Entire Course for Free
[Tweet “Learn how to correctly identify elements using #Selenium #Webdriver.”]How