Dynamic XPath In Selenium WebDriver || Write Like An Expert

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to next selenium tutorial today we are going to cover one of the most important topic in selenium webdriver that is xpath so many people will be aware of selenium webdriver methods how to write scripts everything but they might not be aware of many things that exist in xpath so today we are going to cover all the basics and advanced level writing of xpath right so basically what is expert means so whenever we are loading your html document or your web page into browser so that html document renders in your browser as a html document right so if you are taking any website like this so this is a slip plain html document but if you are inspect this particular web web page right in under elements you can see it's a html document so in your web page it is each whatever you are seeing in this website will be a one web element but when you see over here the html document each one will be considered as a node so this is html html node and head is a html another node and it has multiple child nodes and every element considered as a nodes so to locate a particular pip element so we have to write a locator right so that is called that expert or some other locators which selenium provides isn't it guys right so it is called in other way we can call it as a expert that query export query html documents query so the way how we write in sql query in the same way we have to write xpath query to locate a particular web element right so this is the basics of uh html xpath so there are two different text method one is absolute and next one is relative so today we are going to talk about only the uh relative x path okay so we will be covering the absolute expert in uh another video so today we'll learn about all basics right so let's let's talk about the syntax how uh particular this relativex path the syntax looks like so it will always look like kind of so for relative xpath we have to start with to the forward double slash so that is the first thing that we have to uh know and second thing is after we write this double forward slash we have to give the tag name right and then open angle bracket and to work with any attribute we have to give at the rate and attribute name which attribute you want to work with and this attribute equals within double quotes we have to give what is the attribute value right and then finally we have to close the angle bracket right which we have open and we should close so this is the relative expert that works this is a very basic syntax for any relative xpath right so if you take any web element say i am going to take this username let's copy this this particular web element copy copy the element let's understand how exactly we have to write xpath so this is one web element in html document so this consists few things so one is the html document that is tag name what is the element name and this element this stack contains multiple attributes right so the first one is name and this one is id and this is the type so three attributes this particular html contains and each attribute has multiple values sorry each l of each attribute has one value correct so each html document has its own opening tag and closing tag so if you consider this one right so it will be having our closing tag as well i think it is not having here closing tag uh if you take this one span right you can see over here it's it has a closing tag isn't it so like this it will have one opening tag and closing tag this div has opening tag and closing tag and this div has opening tag and crossing tab so this is one web element so when you write your x path a relative expert we have to start with double forward slash and we have to take this web element name and what is the attribute you want to work with that is after your open angle bracket and at the rate what is the attribute name and is equal to what is the attribute value right so this is how we have to write x path this is very basic actually right so before using this into your script we have to first find whether this export is correct or not so for that what we have to do open your google chrome right click on here inspect or press f12 on your keyboard and it will open here and here in this elements page just press ctrl f and if you press ctrl f you can find here one input text box is coming up so find by string selector or x path right so if you paste your export it will it you can in the right side you can find it is finding one of one so which means this particular x path occurs only once in this particular document so if you give any invalid values right you can see over here it's a zero of zero you will not find any x path with this particular value right so this how you can verify any x path in your browser right so let's start with even simpler than this particular x path so how it will be so any relative expert start with double forward slash right you can see over here nothing is appearing right so i want to locate this particular element so guys listen carefully so i want to uh look at this particular web element that is username input text so what is the syntax is so we have to give the tag name first right so tag name i have given the moment you have given the tag name you can notice over here it is showing one of eight so this particular input tag on this web page it occurs eight times right so if you give this one so it will return in your threading update but the first occurrence of the particular web element not your what your wish to work with right not sure how many are there see okay eight are there right so this particular input tag occurs in the sixth place right so if you want to work with if you don't want to work with any attributes of the particular web element you can directly give the index or in export we can say it's a pretty gate so you can give like an index like so for to give index again you have to open and close brackets inside that you can give what is the occurrence over here it occurred in sixth place right so we can give just six right so if it's not working like this you can just close with paranthesis this particular thing and you can see over here it occurred isn't it so this is the first way of writing your ex father just start with your a very basic very minimal diagonal and attributes to start with right so that way we can understand very easily how we can write it right so we can see over here it's one of one so this particular input tag occurs only once in this page so this is also one of the way to write your export let's put it on though putting put it here this is the one way of writing your x path right right let's see how many experts we can come up with so if you write many experts for a particular given web element then you you are an expert in that right so let's see how many ways that we can write so this is the one way right uh let's see another way like even if you are not giving your uh input tag also the tag name even we can write the x path right so let's let's see that how we can go so for that what we have to give is we have to give after a double forward slash we have to give a string so what will what this will do right it will list on all the web elements or all the single web elements it will return okay you can see over here it is it has written 154 web elements on this page with a string this many nodes are there right so which element you want to work with so i want to i want to work with this particular input tag so let's see when it will okay upper okay occur right let's see you can you can actually keep ion over here which element it comes so it's coming to webpage and it's going over here [Music] or in hrm it's coming it's coming to another hidden element just keep going yeah it has come it has come is it uh i think it occurs in some place where one one seven or something like that it occurs let's put it like this which element it occurs for yes so it appears somewhere else like this let's put it on 115. so it appears somewhere else this is five six seven eight nine and eleven we can give two one right so this is the one element you can see like this also you can write your x path so over here what we have done we have not given any html element or any attributes which uses or any of the display text that uses or any title or placeholder name worry simply we have given only the double forward slash i want to locate some particular element based on this index that's all so this is also one of the other way that you can write your x path right so now how many way how many base we have identified two ways for the particular web element we have identified all right see this is the second way right so and the third way how we can write same like double forward slash i want to locate input tag like this which input tag i want i want this particular input tag so now we will start with uh writing your attribute based on the attribute let's write this so here you can see multiple attributes over here so you can directly write either your id say for example find element by add by dot id you can directly use that but i don't want to use that by dot id so i want to write export for this so the one how can i write all right so we we have to start with if you are working with attribute we have to start with add rate id right so this is the another way so you can see over here the moment you have written id which input tag has id attribute there are seven elements right so you can find like this also this also another way the same way you can write the index over here what is the index it does it has the fifth one right so it has the fifth one so this is also one of the another way so you don't have to give the attribute value directly or if you don't have to use your attribute so whatever attribute which has this particular html tag name which has the attribute id or any name if you wish to use so you can directly use this particular thing that's all this is also another way of writing your x path so these are the some of the very uh basic thing that we we should know so in case of if you if you are not getting uh any values to work with any attributes or display text then this will help actually all right so right that is another way so if you don't want to work with index then remove these particular indexes and instead of index you can give value so what is this value contains its id right so this id contains text username this is the another way of writing your x path so i have given i think a outside angle bracket yeah now we can see this it is the appearance will be like one of one all right so this so this is also perfectly fine to use this so this is paralysis it's your wish whether you can use it or not so it doesn't matter actually still it will show enough one right so this is the fourth way of writing your x path right so the another way of writing x path is the same way how we have written over here for asterisk the second b right so instead of tag name we have used as trick right so here for attribute also we can use the same uh so instead of id you can give aztec so what this means right so you can see what its occurrence only once what does it mean right so the input tag which contains any attribute having the value this one so it doesn't look for your attribute name any attribute with this particular input tag has the attribute value as this one maybe that name attribute can have this text username or maybe that id can have text username or type can have text username as a value or some other attributes like class can have this text username as a value anything anything any one matches that particular web element it will return so that is the meaning of this asterisk over here right this is also another way of writing the x path so this is the fifth way that we we have written the x path for the one element right right so another way we can write over here right so let's take it this as id right so we can we can match with multiple attributes it's not the way that you can always use only one attribute for a particular input tag you can write multiple attributes you can add it multiple attributes say for example some sometimes we have to uh there you want to match with id but this id appears for multiple input not input tag input tags there are multiple uh separation 10 input tags are there all 10 input tags having the same id and the name that its value so what will what what we can do in this case we can look for another attributes where so id contains this text username and another attribute which uniquely identifies this particular input tag which one this input tag which uniquely identifies so in that case what we can do over here is so we can use uh boolean operators like and operator or or operator or not operator so these are three different boolean attributes that uh this xpath supports right so what does it mean by and means let's talk about this so when you are writing like this right so you have to write the first attribute with i at rate id and text username space we have to give an operator it's called operator right like how we usually use in java the same way here also we can write and again if you want to write another attribute you have to again start with like this at the right attribute attribute name at the rate what is the attribute name i have to i want to use attribute name so here also you can see over here it identifies uniquely right so whatever the input tag has id id attribute has a text username and and this uh has name attribute just contains a material that's all right so in that case it also uses this particular thing and what is that this one is text username this also another way of writing it right so this is the another way of writing it and so how we can how does it mean right so this particular input tag must match id attribute and name attribute with these values both should have both succeed the same way how we can use it in java you can just align with that so in in if condition you can use right this and this and this like that so where in java we will be using double ambassador but here we are using directly and isn't it so that's how you can match with this so in the similar way so you can here you can use our operator as well right so we can see here it is matching with one of one right so either this attribute or this attribute any one matches also this will pass the similar way how java works with or this is the seventh wave so in java how we usually write our operator we'll give double type right like this so here we are giving our operator that's all that's the difference over here using the center r right so this is another way of writing the same x path right and we have another operator also not so how we can use that writer you can give and just not operator over here you can give within this so where uh this particular uh attribute name doesn't contain this one so here we have given uh and operator that's why it is not matching anything because this is matching right so i'm i'm just i'm just uh giving this random values just like sample so here name attribute contains six username so that is what hit is me it is matching so some some uh places we tend to use this one not operator where uh this particular attribute doesn't uh this particular tag doesn't contain for this attribute this value should not be there so in that case you can match this particular x path so like that also you can match it so in that case what we can do is we can use not operator isn't it so this is also another way of writing your x path this is a very simple one right so this is also another one so we have covered this and r and naught everything right so this is also another way of writing your x path we can see over here so so for a particular one or input tag we have written the a different x path this is very basic thing that we can write it isn't it so now we can move forward little advanced level so how can we use uh different methods over here right so for that let's take uh this forgot pass forgot your password link right say for example uh in expert writers sometimes it occurs we have to use uh dynamically generated values right so say for example id uh it uh always it occurs like the in the the suffix will be like 1 to 3 something like that numbers will be generated every time but in the prefix it will be like constant so how we have to use in that cases so we can use either one of the methods uh like there are multiple methods of which selenium supports like xpath so one it one is starts with this is the first one and second one is ends with third one is contains method and fourth one is text so these are the four different and very basic methods that we can use right so let's take this forgot your password link so let's use these four methods how we can you utilize over here right so this is the anchor tag which contains href as this value and a display text as this one right or you can take this button also but as of now let's take this one and over here let's write anchor tag so it has six anchor tag in this page i'm not sure what are those but let's work with this only this one right anchor tag we know how to write xpath we have to open uh open and close angle brackets so first let's use our starts with method so anything with which starts with so what is that starts with what will what it will starts with so this hdf is the starts with or let's take uh let's say input tag itself guys so this is not having any attributes over here let's take input it's a button actually so where starts with uh anything i would start with i let's check id attribute right so whenever you use attribute site we have to use at the rate right always whenever you go for your attributes right so attributes you have to use rate id so listen carefully over here guys we have to give comma and within double course you have to give right it is not equal to it's always comma and within double quotes you have to give so what is that which this contains id bdn bdn login right so let's give that right so it matches one of one which which starts with you can write it like this this is another way of writing our expert send it guys so this is the one way of writing your ex path so this is the first way of writing your expert with startswith method right so and how can we write our ends with so this id ends with let's say i'm going to take here install starts with i'm going to take ends with but if you are taking end speed with the same id button you can see over here it is zero zero it is not finding anything right so let's take this uh ends with in right uh it's not finding anything in smith it's not working let's take this i have not verified whether this works or not right ends with method is not working with this id attribute let's come come after sometimes for this guys uh and with id let's come after sometimes id attribute let's take contents method contents method so where attribute id contains what contains let's take button only btn so something's going wrong here yes yeah i think i have given uh double parenthesis and that's why it was not working actually let's ends with still that is not working let's check contents only contains uh iron so you can see over here it is matching three right so what are those this is the one first one second one where uh here id contains this contents that i n and third one is this one this button right so it is not actually uh recommended to use contains method when it has multiple elements with that it's always better to use whether text or con starts with methods right so because it's not actually a recommended one okay so this is the one and if you want to select write based on this one only so then i can go with the index actually like how earlier we usually write like that you can see over here this is finding that particular web element all right so this is the another way of writing your x path right so these are the different methods and we have to see how text works so when using text right so let's take something this one display text so this text work comes like this uh for let's take this anchor check and over here let's take a text and here is a message so not on here guys we we have used comma right here we should not use comma we have to use equals right so this is the equals so this particular thing we have to give so then it will match text is equal to sorry so text equal to this one so we have to give the complete text so this is the case sensitive if you are not giving any invalid values or lowercase right it will not find so you can see over here right zero of zero right because we have not given the right of x path so now we have given a uppercase so it is finding out right so this is how we have to identify so it's a case sensitive the export complete export itself a case sensitive right we cannot use lowercase instead of uppercase or uppercase instead of lowercase so this is the another way of writing your xpath right the the final one guys the chained one actually that is a very uh critical one actually when we are writing your x path so how we have to write uh exponent right so we can give multiple uh slash like that so let's take this a div uh as id so double slash uh div uh within angle bracket we have to use id and if you give it is coming to this point right but i wanted to locate this particular input text that is this one but it is in parent tag so what i have to do is if you want to select the immediate child then you can give just single forward slash and then input right so this finds the next element so right this is the another way of writing and if you don't if you if you are not sure about which child of this particular parent is coming up then you can give double forward slash right so this is also another way of writing the x path and even if you are not sure there are multiple siblings are there under this then you have to filter out with attributes only so in that case what we can do we can use ids attribute or any of the attributes which is available then give the text username right so this is chained on actually we can write any number of uh childs over here there is no uh uh there is no limit to use number of child over here right so if there are multiple tags like this right input tag we have like this and under that we have like this right so even for this also right we can just continuously we can write like so div tag has id like this you button under that we have another div so here you can see two different uh uh child so here i want to select the second child so what we have to do we have to give double forward slash right and then remove this give dip so if you are giving double forward slash only it is matching but if you are giving single forward slash it will not match slit is matching because this has only one development right and then what we can do uh we can give id at rate id like this after that so we have to uh try one more child is it so it's a anchor tag so now it is pointing to the anchor tag directly okay so this is how we have to write so under this we can give a text the display text what you wanted to write is this one so we can directly use this as a display text directly with the xpath or you can directly use this uh particular id has this forgot password link div so both it will click on the link button just for the sake of demonstration right so i'm just i have just written so for this right if you are so if you are giving double dots right it will match to its parent actually so this is also on another one actually you can just note only mostly we will not be using this but if you want to just you can see over here it is pointing to the parent if you're giving slash double dot it's it's pointing to your it's a parent type so these are the multiple ways that you can write the expert um it's kind of a practicing right so how how many experts you can write for a particular web element so in those many times like like you are becoming and slowly expert in export if you know one element if you're writing 10 20 experts for the particular web element then it's kind of you know you are fully knowledged person it's becoming an expert right so this is like kind of a uh writing very basic expert writer using your just the input the tag name just attribute names and using some uh a few methods over here so these are like very basics and the chain expat so there are even advanced level that we have to use x for the access so we will see that in the next video so i hope this video was helpful for you and you are able to learn something in from this video right so please subscribe to my channel for more videos guys thanks for watching
Info
Channel: Automation Testing
Views: 251
Rating: undefined out of 5
Keywords: how to, dynamic, xpath, expression, function, method, contains, syntax, partial, text, attribute, xpath contains, xpath contains text, xpath syntax, tutorial, selenium, webdriver, selenium webdriver, xpath tutorial, xpath in selenium, xpath contains partial text, xpath contains syntax, xpath contains text syntax, xpath contains class, xpath contains exact text, xpath in selenium webdriver, dynamic xpath in selenium webdriver, xpath axes, locators, locators in selenium webdriver
Id: XyAU1HEg2cY
Channel Id: undefined
Length: 33min 0sec (1980 seconds)
Published: Sun Jul 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.