Dynamic XPath in Selenium using Different Approaches

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to learn automation comm this is Mukesh today we are going to talk about XPath in detail so we will talk about simple expert dynamic XPath and we have different methods available for XPath so I will break this expert tutorials so today will focus on some limited methods so let me show you what exactly we will cover in dynamic XPath so we will talk about the basic syntax that is the you can say basic syntax that you have to remember always because this that is the base for XPath then we talk about end operator and or operator because sometimes you will get more than one attribute to find an element so we will talk about this and n are then the most frequently used method called contains and while writing dynamic XPath 95% you will be using this contains method so will use start with method that is again method by XPath so this is also very useful when you have a fixed pattern then we will talk about text method if you have some text that is displayed on a web page so if you want to find that element using that text you can find that element so in the part one I will be covering till this and the next part will talk about some more advanced method like following sibling preceding sibling ancestor absolute versus relative path okay so we having a syntax for them so I have covered everything on my blog so so this is my blog and this is a post which I have written like how to write dynamic XPath and selenium and I have written every possible syntax so let me start with the first one so we'll start with the relative XPath later on you will get to know what is absolute XPath as well so let's start with a single attribute so as you can see we having this syntax called first you need to write double slash here then you need to specify let me open in a separate window so it will be useful so this is double slash double slash means you want to search for a specific element throughout the page so you need to specify the tag which tag you are looking then this bracket at the rate symbol then attribute name what attribute you are specifying equal to in single code you need to specify the value for that particular attribute so these are some examples I will show you in the live page as of now let's stick to the example let's say I have an anchor leg and that acre tanker link having one extra attribute and that values HTTP colon double slash www.google.com so what it will do it will find an anchor link throughout the page which has one H of equal to this okay so if it takes another example so here I am looking for an input element whose ID equal to name so it will match all the element throughout the page whose ID equal to name so this is another example and again I am searching for an input tag where name equal to username we'll talk about this attribute so just give me a couple of minutes we will discuss about this name IDs and all so in the last example I have let's let me search one image so I am Jesus separate tag in HTML and I am looking for one image where alt attribute equal to this so let's start with this first one so this is the website which I will be using for the demo purpose even you can have this website on your browser simply type demo site dot centers slash WordPress less WordPress login dot PHP so this WordPress is just in software which allow you to publish your blog over the Internet even my blog is hosted on WordPress so this is my blog and I hosted this blog on WordPress so this is just a dummy site so here we have some username password this radio check box button some link so let's use the first single attribute let's search for any specific element so this is one extension which I have on my firefox called Firebug and fire path so let's say if I need to search this element so you can see I am having so many attribute for this so I have an ID called user underscore login I have one class I have type I have size I have value I have name so you can see the beauty of this fire path it is automatically generating this expert for you which is nothing but double slash star so they generally use star it means they are not specifying any specific element they are searching all the element on this particular page where ID equal to user underscore login okay so let's make it more specific I will delete this I will simply say double slash I know this is an input tag so I will simply type input and I will say add that it this is a syntax so you have to remember this then which attribute you are looking so let me search for this name so I will say at the rate name equal to single quotes and specify the value okay you can see it's matching with one element if you put mouse over here it will highlight that element it means the XPath which you have written is correct so I will show you multiple ways to write this XPath so let us copy this and let me start with notepad I will keep a note of it so this is one XPath now if I need one more XPath for this so I know I have ID as well and ID is equal to this so I will change this and I will change this to ID so you can see again I got one more matching matching node here you can see in the left corner and if I put mouse over here it is I highlighting that element so it means this is our another XPath in the same way you can start exploring this XPath you can take any element now if you take this class say we are done with ID we are done with name so we are getting one unique element now if you take this class what will happen let's see so in spite of ID if I say class and if I say this input okay you can see it is still matching but this time it is matching with two matching nodes in the left hand side here you can see a match it is matching with two nodes so one is this that is nothing but your username and it is matching one more that is password so if you notice both having this class equal to input so as per my XPath it is simply matching to element where class equal to input and I will highly recommend do not use class okay because class will not be constant and it will not match with the unique living so I will highly recommend okay like go with ID name if it is available in worst case you can try with class so this is one example in the same way you can explore other things as well let's say this button for this button I have one ID called WordPress - submit I have value equal to login so yes let us see I want to capture this button so you have no ID you have name you have value as well right so let me take this value so even though now if you think little bit for the future purpose might be they will change this ID right that is WordPress - submit may be they will change the name but value I don't think they will change because login is the very frequent text generally we used for buttons right so in this case you can take this value and once you move forward chances are very less this value will change to your XPath will remain same so I will say at that value equal to and you can see it's matching if I put mouse over here in the same way I you can start exploring for this you can see I am getting very very XPath right for this I will tell you how to write this customized XPath so I will give you this in this as an assignment you can start exploring it so what we'll so using this method try to identify all the element on our webpage let's say if I need to validate this logo so if I need to find out this so I can see I am getting one title here called powered by WordPress I am getting one H ref as well and I am getting some text but if you see the XPath which is generated by a fire path it doesn't look good and I don't think it's reliable as well so if you need to customize this as well you can say find an anchor links you can see is an anchor link here and title is this so I will say search for an attribute where title equal to in single quote I will specify this value congratulations it's matching and right now if you see this XPath looks quite good it's not giving you H one and a and all so like this you can create your own XPath because for the long run this expert will help you so if you take this expert which is generated by a Firebug in fire path it doesn't look promising and it may fail in future because it contains so many other things as well so well hey I will highly recommend take some time start writing your own XPath so once you run this script for the future releases as well it will be same so I think you must uh you all are familiar with this single attribute so let me move forward with the multiple attribute for the multiple attribute it's nothing much so in this post I have taken Gmail example so you can try with any application so let me show you the multiple attribute one and if you see here almost same but here you can specify more than one attribute so if one attribute is not sufficient to identify a particular element you can take help of another attribute to specify that element and I think we have a very good example here so let's say if I want to identify this password as a flaw let's say I have one only I have only one attribute called class so if I say double slash input and I will say class equal to this so I can see it's matching with two nodes and I can see now one attribute is not enough to identify this element so what I will do I will try to take help of another attribute and I will say this find an element where class equal to input and name equal to log and you can see it's highlighting it's very straightforward in spite of giving one attribute you can give multiple attributes as well and it will match and in this way you can specify and and or operator as well that's very good feature of this okay so sometimes let's say if you are specifying more than one property so let us say one property is getting changed and still you have one more property to identify that particular element right so let me show you you can directly specify or here okay so you can see once I say or so it simply says either this or this right so right now it is matching with both the cases so it's giving you 2 matching nodes but if I say and definitely it will return only 1 because I am specifying it should be class equal to input and it should be class equal to log right sorry name equal to log so it will match only one element and if you specify or it will search for either this or this right so this is how you can specify multiple element sorry multiple attribute and you can find that element because main tasks of automation identify the element perform some operation and validate your what you can say output right welded your response so this is one of the major tasks that you have to do in automation in qtp you have object spy so you don't have to do all this but in selenium you need to write this expert by your own but it's quite interesting so I always enjoying writing XPath so this is another example I will specify this with or and and here it's not case-sensitive so you can use capital letters Miletus okay so the next one is very important very very important is contains so here as the name itself says it will not check the exact contain content it will search for the partial cannon so let's say I need to search for an let me login first admin password is demo 1 2 3 so even you can login from your system password is admin so now it is the typical homepage and you can see I have so many elements here I have home update post media link page these commands appearance plug-in user tool settings and other things as well so let me start with identifying one important let's say I need to search for this I'm not getting the dynamic ids it's a very fixed one okay let's use this so here I can see this is one text box so I am getting tagged equal to input I have ID equal to title and name equal to post item so if I use contains I will say double slash search for an input where contains so contains is a method and you need to write at the rate here and which attribute you are looking so let me say I am looking for name attribute and name which I am taking is post underscored title so it's not matching because it says contains but this is the exact match which we are getting so here you can use direct contains will look for the partial one so this is not the good good example for this here we have the exact one so you can use this post underscore title let me search for another which having the dynamic IDs yet this sounds good so here you can see I have one H ref right H ref is redirecting to added dot PHP and if I identify this using simple this fire path so it's giving very lengthy XPath and it doesn't looks good as well so let's try this I will copy this H ref and let's see how many elements it's matching so we'll search for will say contains at the rate H Rev because we are looking for H ref and so you can see I am matching it's matching 13 nodes if you see here one two and so on right it's matching so many element which contains H ref equal to added dot PHP so as you can see here even though it having so many lengthy but it is only checking for added dot PHP so now this method is working fine so here you need to add some more attributes so that it can identify unique element right so here let's try we'll search for one text so text is another method what it will do it will search for a specific text so right now I specified search for an element where it contains H is equal to this and text equal to post so now you can see here it's matching with two nodes only so one is this and one more one more is it will be somewhat here yes this is the one okay so this is the one and one more on the top this one but we need this one so here it won't think this will work zero and one still matching with two so now I'll just keep I here what we will do I will keep this example for following now this is the best example which I got right now right now you can see here I'm matching with it's matching with two nodes right but we need only this one so what we will do will search for content then we'll search for this okay so here let us check whether we have some more elements or not no we do not have any other thing it contains class equal to null and the first element and it does not yeah it has class and it is having very lengthy class so let's search for a class as well no class is blank so it is not going to work so let's take this example for the following one and if you want let me complete this so this will be little bit at once so definitely we are going to cover in the next tutorial but as of now because I do not want to keep you waiting soul let me search for a texts called contains I will use star because I want to check the content and I will search for content content only so you can see I am matching with this content and discontent so again we are matching too so let's search for this discussion so again we are getting to discussion now let's search for this so I will use following it means after this whatever elements we are getting just search for all these elements and here we go so now you can see it's matching with one node only and here the main role of following comes into picture so anyways I am going to cover this in the next tutorial but as of now what I did so this particular XPath was matching with two nodes so what I did I searched for this discussion text here using this method then I said following following means after this discussion whatever elements are coming in that element search for an anchor tag where H ref equal to this and Tex equal to this I can understand is going to quite it's going to be quite complicated so I will keep this for the next part because first part we will focus on the basic examples so contains I think you you got it right let's search one more let's say this is one text is coming dismiss this message and I can see here this XPath is not reliable definitely if I run this script more than three times it will fail but I can see here I am getting one message called dismiss this message so we can search this element using this method I will search an anchor tag which contains text , and in single quotes specify this and congratulation is again showing one matching node it means this XPath is valid and if you put mouse over here it is highlighting this as well so like this you can specify any element and you can search for this and let me give you a very good example right now in this application we don't have let us say you have a text box where ID is changing dynamically let us say right now IDs edit 1 oh let me start yeah let us say edit is 1 for IDs edit 1 5 4 5 so next time when i refresh this page ID comes edit 7 8 9 8 9 if I again refresh it it will say edit 1 2 6 5 but I can see I am getting 1 pattern here called edit is constant remaining thing is getting changed right so now I have flexibility I can use one method call start with I will say search for an element since I am not sure about this so I will say star contains at the rate ID comma in single quote you can specify this edit okay so I will use this example again for another method but this is one very good example since you can see this is dynamically changing but this fun part is fixed so you can identify this okay in the same way you can explore it you can try all this from your end let me show you some more example from a blog that I already listed so contains now let us say you want to search for any specific anchor link which can change which contains a chip that we already discussed let's say you want to search for an image whose SRC is equal to this and you want to search for an div tag which contains ID equal to this and so on so like this you can start exploring this method it's very useful and you will enjoy using this method so once you are done with this part I will suggest you start exploring on this particular website or you can take your company application as well right so let us take another one called start with as the name itself itself says it will find one specific pattern only at the starting so if I take again this is the example I can see one part is fixed so I can use contains or I can you start with as well so I will say starts - wit this is the syntax at the rate ID equal to adit okay so if you get if you get the specific pattern like starting part is not getting changed you can use this so it will search for an element okay where I D start with added so I have listed so many examples here you can have a look search for an ID which where ID start with this where I should have start with this and so on so we have now following sibling and the absolute X paths so that I will cover in the next part but one more important that I forgot to cover in this part let me search for an element based on the text anyways we have seen using contains right but let's search for the specific text so let us say you can see here I am getting one text called Howry admin so let me give a try here so what is the syntax double slash if you know if you don't know the tag you can keep a star search foreign text kick parentheses equal to in single quotes specify this and here you go you can see I am getting one matching node and if I put mouse over here it is highlighting let's say I want to search this particular link that is please update now again this XPath is not going to work for the long time so I will use this text again and this text again help you this XPath will go for the long run until they don't change the text so if you are working with text based elements go with this you can again see it's matching with one matching node so it's quite interesting and it's not like it's a fifteen minute or twenty minutes tutorial you need to explore it from your end you need to keep trying try to identify all the elements using these methods and it's totally up to you like which method you want to use there is no specific rule like you have to use this only so you need to find out the best XPath based on your application so you have a Liberty to use so many methods here we talked about sin basic syntax and then our contains start with text so in the next part we will be focusing on the next four even if I start a telling expert for the different element this session is not going to be end because it's only practice practice practice so start practicing from your end in case if you find any issue just let me know if you had any doubt you can contact me so thank you everyone for watching this video we'll see you in the next part have a nice day bye bye
Info
Channel: Mukesh otwani
Views: 275,231
Rating: 4.9400854 out of 5
Keywords: XPath (Internet Protocol), Software (Industry), Selenium (Software), Automation (Industry)
Id: As72m9qrAQ8
Channel Id: undefined
Length: 28min 12sec (1692 seconds)
Published: Sun Oct 18 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.