Here which is almost correct actually it just needs a couple of small tweaks to get this to work to demonstrate the technique i'm going to use a site called the internet and.
The large and deep dom page on there we'll begin by getting a reference to what we'll consider our child element so we'll get a reference to this.
Specific element with the text 3.2 and then we'll work out how to refer to his parent and maybe even the grandparent and great grandparent elements from there as well.
To get started i've set up a blank excel workbook with a bit of code stored in there already so i've written some simple code to fire up a new instance of google chrome.
And then navigate to that url first we'll get a reference to the child element so let's head back to google chrome and then right click on that element.
That and then copy that to the clipboard and then head back to the visual basic editor i'll declare a variable at the top of this subroutine to hold that child element i'll call it ch as a.
Selenium.web element and then after navigating to that url we'll say set ch equals cd dot find element by id and then in some round.
Brackets and some double quotes just paste in that id that i've just copied i'll close the double quotes and then close around brackets and then just to.
Demonstrate that it's working let's debug.print ch dot we'll go for tag name perhaps and then ch dot attribute and let's print out the id attribute we.
Should know what that is of course it's the one that we've just used to get a reference to that element but just to demonstrate that that part's working so far.
Let's run that subregime we should get a
New instance of chrome navigating to that page and then not very excitingly we'll get the tag name and id attribute.Of that element printed to the immediate window next we can get a reference to the parent element of this child and just so that we know what to expect.
When we do that let's head back to the open version of chrome and find out what the parent element of 3.2 actually is so if we head over back to the list of.
Elements on the right hand side and trace our way upwards from there we'll see that the immediate container for this one the parent of this element is another div with an id of sibling 3.1.
So as it turns out we already have a really convenient way to reference the parent element of sibling 3.2 because its parent has a unique id as well just for the sake of demonstration.
We'll pretend that that id doesn't exist so we don't have a really convenient way to reference that element uh initially it's worth mentioning that each element can only have one parent.
But can have multiple children so the sibling 3.1 element is the parent for the one we've already referenced sibling 3.2 but it also has two other.
Children sibling 3.3 and sibling 4.1 as well anyway to get a reference to the parent of sibling 3.2 let's head back to the visual basic.
Editor and i'm going to declare another variable let's say dim pa short for parent as a selenium dot web element and then after we've printed out the details of our child element.
We're going to say set pa equals
And then we're going to refer to the child element as our context node so our initial starting point for the next search.We'll enter a full stop and then we're going to find element by x path so this is uh the most convenient way or in fact the only way i know have to.
Refer to a parent element of a child element using an x path option so if we open some round brackets and then in some double quotes.
We're going to refer to the parent axis and then two column characters followed by the word node and then open close round brackets close the double quotes and then close.
The round brackets again so this is using the full complete syntax to refer to the the parent node so the unabbreviated.
Syntax having done that i'm just going to copy and paste my debug.print statement and then replace ch with pa and then clear the contents of the.
Immediate window and then run that subroutine again and find once it's finished then we should now have two items printed into the immediate window.
The div tag of the child and the div tag of the parent now as well as using this full unabbreviated syntax to reference the.
Parent node xpath also provides a shorthand version of this or an abbreviated syntax so we could replace parent colon colon node and the open and close brackets.
With two full stop characters which as i'm sure you'll agree is a lot easier to type in although a little less obvious i think to somebody else reading your code what.
That's designed to do anyway the end result will be the same if i clear the contents of the immediate window and then run that subregime we'll find.
That we end up with the parent element of our child sibling 3.2 xpath selectors also allow you to provide a number of steps.
To find an element so for example from the parent element of our child we could add another step to search for other elements from that point onwards this kind of brings me back to jason's.
Original question so the sample code that's provided there there's a full stop followed by a forward slash and another couple of full stops so we know the double full stop refers to the.
Parent element but the single full stop there refers to the current context element so it's a little redundant actually if i type in a full.
Stop and a forward slash that's just saying from the current context element find the parent element so that's just exactly the same as saying.
Just the two full stops find the parent element but just to demonstrate how that works if i just get rid of the immediate window.
Or the contents of the immediate window i should say and then run that subroutine again we will once again end up with the child and parent that we've already seen.
So while that's not particularly useful what could be useful instead is if we wanted to find the parent of the parent so rather than having a single full stop.
There we could type in two full stops to find the parent of the current context element and then add another step with the forward slash character.
And search for the parent of that element so rather than showing the parent element now our code is going to show the grand parent element and again just to make.
Sure we're happy with what we're about to see here if we head back to the open version of that webpage so we've found sibling 3.2 is our child sibling 3.1 is the parent.
And if we go upwards again from 3.1 we'll find that the next parent the grandparent of 3.2 is sibling 2.1 so if we run that subroutine again we.
Should find now this time that we get 3.2 and 2.1 as our elements as you may well have guessed you can indeed add yet another step to find.
The great grandparent and great great grandparent and so on just by adding another forward slash character and another couple of full stops.
So once again if i just clear the contents of the immediate window and run that subroutine we'll get the our original child and now the.
Great grandparent element of that child there are many many more things we can do with ex-path selectors it's a entire language in and of itself so i think that's probably.
Enough to answer the original question that jason asked but just in case you are interested in finding out more about xpath i'd recommend looking at the x.
Path language specification i'll drop a link in the video description so you can find this a little more easily but the path expression section covers everything we've talked about in.