How to Become Genius in XPath in Selenium - Session -18

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
otherwise welcome back to selenium training again yesterday I'm going to cover I'm going to tell you one very very interesting thing that easily you can you know you can easily you can learn and you can easily you can write that how to design a particular expert any complex XPath also you can design okay I'm going to tell you very simple trick and especially for complex scenarios especially for complex you know XPath you have to create then how to do that okay so before writing this into the script and we have to make sure that okay that XPath is perfectly fine or not we will check on the Google Chrome and then we will okay right into our script okay so that okay let's do it if you practice properly with this thing I'm telling you guys you can be a master in next month okay but a lot of people they're asking me that or gonna be in that I'm not able to create that expert and you know having some problem creating the expert they know selenium concept and everything but the problem is they are not able to create proper XPath okay the proper customized dynamic expert how to create that a lot of people are asking about that so today I'm going to tell you that trick okay that how exactly I created okay so first what you have to do now forget about Firebug and fire path okay on Firebug and fire path if you open this particular site and if you if you click Lachie if you open any particular site and if you aspire but in' on any drop-down or whatever the problem here is that it's it is give it's giving you the complete a you know XPath and ID and everything but here you cannot verify okay on Firebug on fire but you cannot verify your customized expert that your customized x4 is correct or not that option is available here in chrome so I'll open my chrome and let's say this is my site fee free CRM dot-com and what I'll do let's see I want to create the expert for username so first right click on it and go to inspector okay and what I'll do if you see this is the complete HTML code is displayed over here and this user name field is represented by input field and if you see this input type equal to text class equal to this place hold or equal to this name equal to username so easily I can use by dot name okay easily I can use by dot name so let's see else if you don't want to use by dot name I want to use by dot X path so how to create the X path of username okay how to create the X path of username right so how to do that so let's see very simple example first you just need to press ctrl F if you're working on MacBook you have to use command F so just press ctrl F and you will see that one this find by a string selector or X path option will be displayed over this this field will be the displayed over here and let's see I want to write that okay I want to write the X path of username field what I have to do I know that my expert should start with a double slash so I will see guys focus over here I'll write with double slash then what is the HTML tag the HTML tag is input C this is the HTML tag HTML tag is input so I'll write input okay input tag now if you see the count is it showing one of three it means total input tags are available on this particular page three so this is the XPath of input very simple now I'll slash input it means how many inputs are available three inputs are available on this particular page but with this input I want to add some properties well write input which contain at the rate name is equal to the name is username name and the rate name is equal to username how many elements are available only one element is available if it is saying one of one then only we have to use these always remember if it is saying 1 of 1 it means this is the exact expert we are looking for okay input at the rate name is equal to user name in single quotes if you are using at the rate name you have to use equal to if you are using contains you have to use comma great guys so it shows that we have an input field where at the rate name is equal to username okay only one input field is available on this particular page so this is a very simple very straightforward very basic things we have done great same thing for password also if you want to write so I will aspire this password and I know that ok the password name is password so I'll simple write input at the rate name is equal to password again it is showing 1 of 1 it means this is my correct XPath so if you don't want to use bio dot name you can use by dot this particular XPath you can use fine ok now let's create the expert for this login button so for login button if you see I cannot use by dot type by dot value or by dot class I should not I cannot I don't want to use this I don't want to use because there is no by Road ID also n by dot name also is not available then obviously I don't have any choice and I have to create the XPath for that so simple same thing double slash what is this input tag what is the HTML tag HTML tag is input simple Android input ok and then you have to put these two brackets square brackets input ok input and then I have to define my attribute so how many attributes are there see type value and class so I can use either of them or combination of multiple end okay multiple attributes also I can do that by using and operator so let's see I want to use input at the rate I have to start with at the rate type is equal to submit ok so you can use this X but because it's winged one of one means it is only there's only one element is available which where input at the rate I pick well to submit okay so if it is showing one of two or one of three guys in that case you don't need to use that expert because there are multiple elements also available ok with the same expert in that case right now let's see if you want to make it more reliable XPath then you can use one end operator simple you can write and and you can write at the rate value is equal to log in see this value also add the rate value equal to login so at the rate type equal to submit and at the rate value is equal to log in and if you want to write at the rate class equal to VTN also you can write like this okay something like this so this is a expert fort login right within within seconds okay within the second time creating expert base you can use this expert okay now let's see some more let's see for example this is a link right I don't want to use bio dot link text and here I can I don't have any property there is no by dot ID by Road class named by dot name nothing and the next part is also okay I'm not able to find so how to create the XPath for links simple double slash what is the HTML tag HTML tag is a and write a okay so if you see HTM double slash a means one of nineteen it means nineteen a tax nineteen links are available on this particular page 19 link space okay so let's filter it out a give me all the a what what do you do what do you want so I want a which is having text so text is a function okay so I guys I cannot write at the rate text so with text I have to write function like this text these two brackets like this comma what do you want I want a text okay comma features right features F e 8tu arias features okay just a minute okay ad okay a text is equal two features right 8x is equal two features so like that also you can use okay so I'll do one thing guys create one file notepad file so and store all these experts for you guys okay so this is for text I have it written I mean this is what link sorry this is what link this is expert okay so without contains I'm using now let's see with contents okay guys now see you'll be the content so I will try double slash a square bracket I'll use contents okay because content is a function so I have to write these two brackets contains what a particular a contains text so guys if you if you don't want to use contains guy see if you don't want to use contains you have to use text is equal to but if you are using contains you have to use comma and the value I am expecting features so how many elements are there only one element is available so I can use this XPath also so in this XPath also I can use okay so for links I am creating this is the first expert and this is the second expert so I can use either this or either this let's see some link Texas like this features of selenium right so in that case if you don't want to use 8x is equal to features of selenium you should write a contains features of selenium you should write contain so I always recommend to use contains okay like fine okay let's see some more complex examples okay let's see for this button sign up button so I despite this and this button is like this okay so for this button the text is written like this so I will create the X bar see guys double slash this is a button okay and there are four buttons are available on this particular place double slash button means there are four buttons are available okay in a square bracket and okay so what I'll do this is the button and what was the property I can use I can use first properties type equal to button so simple I'll use at the rate type is equal to button but again you will see in one of two one of two means there are two buttons are available on this particular page which are having type equal to button so this is the first button see it's showing this first button somewhere I don't know where exactly this button is available but there is a button okay maybe there is some button or some hidden button is there okay so there is one button is there okay it will type equal to this and if you press ENTER it will become 2 of 2 okay so there is another button okay this yellow highlighted line is okay is representing the HTML code right so button at the rate type equal to button if you write so I cannot use this particular expert why because it is showing two elements are available on the page great so now what to do I have to make it more more robust so I have used type equal to button okay and let's say I'll use class equal to BT and also so what I'll do simple and ruse and operator and at the rate class is equal to VTN okay now it is fine see it is only showing only one of one means only one element is available okay see if you spy this on the yellow line if you aspire if you powers over it will representing this sign up now I can use this XPath for sign up button for this button right and let's see if you okay so this is another type so for button this is one now I want to use contains so how to use contains base double slash button contains contains water see if you spy this particular pattern contains sign up link sign of text right you can see that sign up text so I'll copy the sign of text which contains what it contains text okay and if you are using contains don't need to use no need to use double and I mean equal to simple use comma in single quotes write the text so again it is showing one of one so I can use this particular expert also okay so which one you prefer I would prefer to use this one contains one although both are perfectly fine you can use either of them see I am creating my own customized XPath cried race okay so let's get into this application know be in ek and the password is test at the rate one two three and let's see some other ok let's see I I want to get the XPath of this particular this particular link okay this is very simple these are all links so let's say I want to get the XPath of let's see alerts so how do you do that simple I'll use double slash a okay see there are 370 a ami 370 links are available 370 and then I'll use contains okay contains what do you mean by contain contains what contains text comma alerts C now C contains alerts guys so they are one of three it means there are three alerts links are available on this page so if you want to see what are a different alerts option are available 300 so I cannot use this so a contains text alert so if you this is the first order T okay where is the second alerts just pressed enter it will move it to two to three two of three so this is the second of it see SMS alert this is your second order because I have used contains okay so this SMS alert is also a link which contains all notes okay so sometimes alerts if you are using contains you have to be very careful sometimes two of three third one is press enter third one is this alerts and campaign final beta see alerts SMS alerts and campaign's final beta and particular text also alert is available so contains will check only this the complete text is having alerts or not okay so in that case alert is not recommended so I will remove this contains from here and simple I'll use a what which contain text and I don't want to use contains I'll use a contains text is equal to alerts very simple very straightforward so this is for link again okay so if you use contains and there are multiple options are available multiple elements are available in that case we have to use without contains like this okay so this is okay this is a thing I'm using for expert creation right now let's see let's open some other application just a minute okay let's open this application okay so not this application it is just a minute and give you better example let's see this example okay and on this example I want to okay so if you spy this this particular drop down you will see that by dot class you can use ID you can use let's see there is no ID then you have to create your own expert okay so how to do that so simple I'll use double slash button okay so if you see double slash button means there are 37 buttons are available 37 buttons are available let's see this is the first drop-down button now I have to get the XPath of this particular drop-down so how to do that button okay button bracket and what are the different properties are there four button for this button let's see I'll use let's see ID okay let's say I don't want to use ID let's see there is no ID so first I'll you type equal to button so very simple at the rate type is equal to sum' button so with at the type equal to button also there are 37 elements are available fine but again I have to okay refactor it fine and it is saying let's say I want to use class simple I'll use and at the rate class is equal to this class now see nice in for this button there are three classes BTN face beauty and secondary space drop-down toggle so I cannot use this particular class like this nice okay see yesterday's is showing that a particular button is there where at the rate type equal to button and at the rate class is equal to this still there are five options are available five elements are available having the same property type equal to button and trans equal to like this so again I have to make it more gender ik okay so that I can get only one of one the exact X path of this fine this is also not working now what exactly I'll do I see that this button is coming under this particular div variant ID so I'll do one thing I'll create one parent ID Dave like this okay so I still it is saying that okay div and under that particular button is available under that particular button is available but what I'll do for this div I will create one attribute class is equal to class is equal to drop-down I'll define this drop-down class property over here now it reduced from 5 to 2 it means there are still 2 elements are available which are having the same XPath okay so div at the rate class drop-down button this so the first one is this okay the first one is this and if you press ENTER the second one is this okay second one is this okay so both the buttons are having the same property okay both the buttons are having the same property this is the first button having the same property under this div div class equal to a drop down under this drill one button is available if you see the second under this div class equal to drop down this is the second button having the same exact property same class and same time equal to button the only thing which is segregating that is ID okay so let's see I want to include Eddie ID No okay so there is one ID okay like this so what I will do I'll use one more end up and operator and at the rate ID is equal to like this drop down menu button now it is one so this is the X part of this particular button you can use you see this is the X part of that particular but are having three properties button type equal to button and rate class equal to this and I it is equal to this okay so see although it's not compulsory guys you have to use this expert okay although it is not recommend it I can use very straightforward this is just to show you guys it's very straightforward or to do that simple I want a button having at the rate ID is equal to this drop-down menu button very something you can use this X card directly see one of one it means only one element is available okay so I can instead of writing this long expert although this will work this is also will work okay but the performance PI's which one is better performing fight this one is better days why because it will see if it will go to first this part if you live under that particular to give it will go to button and then it will check all these three properties and then it will click on that element but here it is very straightforward simple it will go with that particular button where ID is equal to this okay I it is equal to this like that so like on Google Chrome first you can check over here you can inspect any element okay and then let's see this is all this is link right click on it and simple click on inspect you will see that a tag is available and see if it this a tag I can I don't want to use this class because there is no guarantee that there are multiple links are available having the same class Hof also we don't use on-click also we don't use I can use only bootstrap text is available so simple first I will verify over here double slash a contains okay and text comma this particular text so still there are four elements are available where a contains two X is equal to bootstrap so I've removed this boot contains from here okay and then I'll pass equal to this and now there is only one bootstrap it okay link is available which is highlighted by this yellow name this one so easily you can write in okay a X equal to bootstrap same thing for proceeding expert nan proceeding X path that I covered okay in the previous session for dynamic handling of you know dynamic web table handling and all those stuff plus reading XPath also you can verify over here okay so this is a thing nice let me give you one more example if I can just a minute okay I'll give you one more example of proceeding okay expert proceeding in proceeding sibling expert see it very carefully especially for you know map table handling okay what I want to do I want to click on this particular checkbox okay however you do that okay with a single shot how do you do that simple you need to click on it so if you get the X part of this there are multiple checkboxes so selenium will be confused at on which expert I have to on which checkbox I have to click on it okay so simple how to how to get the exact XPath I am NOT talking about greater relative XPath how to get the exact XPath of this particular checkbox see the relative XPath of this checkbox will be like this if you right click and copy XPath so it will be like this the relative expert will be like this guys okay this table tbody tr6 and td1 and input for tomorrow HT t instead of six throw that okay instead of six throw this particular element it's coming one six row now it is coming on seventh row or eight throw so it will be TR 7 or right it will be TR 7 or TR 8 or TR 9 so I should not use this particular absolute expert this is absolute expert I should not use that okay so how to get the customized expert how will you create the customized expert now see I know that I have to select the expert for this element which is coming in front of just before the test - test - okay so what I'll do I'll spy test - test - test - test - is a link right so what I'll do I'll do some experts engineering that River in the reverse mode I'll do the expert what I'll do this is a who is a parent of a the parent of a is this TD and who is a sibling of this TD the sibling of the CD is this TD and who is a child of this TD input okay so i'll strivers like this first i will go to i'll i'll go to a and reach at a from a - TD TD to the brother of this particular TD is this TD and then the child of this TD is input and then I click on it okay so how to do that guys see very very good example if you can get expertise into these things you are you are you can do anything in selenium ways okay so simple as I told you first I have to reach at a so double slash a which contains what which contains text comma and this is what this is takes to test - so a which contains or I'll write which having text test to test - fine so sorry I'm not using content so I have to use equal to equal to test to test - so okay how many elements are there one single element is available now what to do I have to each where I have to reach the parent of a so I'll use double slash parent okay this parent property and use in expert parent and the parent of what Steve whenever I'm using property in expert I have to use parent like this pennant is a property and to school and I have to use like this so and repeat : : ok : and : and what who is a parent parent is TD fine so we are still we are working fine one of one element means we have reached at this particular TD right so let's see there are two parents in that case what I will do I will define the property of TD TD also to make it more reliable more robust where class is equal to data slide row so I will use TD where at the rate class is equal to data less true fine so we have reached at the parent of a a to TD see I am going in a backward way okay from child to parent M preceding and then from a two-parent TD and who is a brother of TD so there are two brothers of TD let's see this is the first brother and he is a second brother but I don't want to interact with this particular brother what I want I want to interact with this sibling this brother so I switch from this TD to this TD right so how to do that simple I use double slash I'll use proceeding sibling so again one more property is there preceding sibling guys if you want to reach at this particular TD then you have to use forward sibling okay if you want to use okay before this particular TD okay so there are three brothers brother number one brother number two and brother number three and we have at this particular brother number two so if you want to move to first brother you have to use proceeding okay if you want to use the second 3rd brother you have to use forward okay so I am using preceding sibling again this is a property I'll use to : : : and who is preceding sibling mr. TD and use TD and what is a property of TD I'll define the property of TD that TD is and that it class is equal to again data list wrote this is a class i right okay guys data list row fine so i've reached where exactly I have east I have really still here okay tail air I have reached now I have to jump to input simple I have to jump to input and input is available in this particular TD this is a parent and he is a child input is a child simple double slash input simple and let's see there are two inputs input 1 input 2 there are the leti in the same cell there are two checkboxes are there okay so input where name is equal to this contact underscore ID so I'll use input at the rate name is equal to contact underscore ID right what how many counts only one con it mean this is my exact XPath of this particular okay check box so see the expert that is this is the expert I'll be using XPath of that particular check box isn't it amazing that instead of writing to four loops you will either eighth row by row in everything you will do those drama unnecessary simple you come over here and use by dot this XPath and dot click so what will be a code simple driver dot find element by dot XPath and this is your expert and dot click simple that's a statement you have to use very simple and this dot click it will click on this particular checkbox right if you want me to write the code and quickly I'll write the code for you guys ok so I think that till contacts which I have already written one code in the previous examples if you see yeah I will create a class and let's see dynamic XPath and select this main method okay and what I'll do and copy from here to click on this coming on this contacts page right till here I'm coming over here coming on the contacts page you guys you can write this code very easily enter the username password and coming on the contacts page okay on free CRM dot-com site and after coming on the contacts page what I want I want where it is yeah I want to click on this test - test - I want to click on here so simple I will be using this expert ok I'll be using this code is try or dot find element if you see no I would not find any mint see this is the see this is a beautiful customized expert I have a written by my own and I proud of myself that I have written this XPath if you have this kind of expertise you can do anything in selenium selenium is everything is all about 90% case is all about expert how exactly and how dynamically you are writing the expert ok so I really proud of myself that I am pretty much comfortable with expert and they will ask you at a time of interview also easily you can automate any any xinru any damn scenario you can automate in selenium if you are good in expert if you learn these skills peer in property I'm using parent property proceeding expert ok from first time you know moving from child child to parent and then parent of this guy is another parent it's like see I have okay I'm Naveen and I'm going to my parent my father my father brother is my uncle and from uncle you know uncle will be the sibling of my father and my uncle's are childless input okay so I'm reaching over here so if I have to reach my cousin okay so these input this input and this a both are cousins so if I have to reach my question how will I do that so I will go to my father from my father to my uncle and uncle to present like this same thing okay this is Naveen my parent is TD from this TD - he is my uncle TD and from uncle tu to input okay he is my cousin same thing is available in this particular period TD from this parent TD - I am going to proceeding sibling means okay the big brother you can say like that you can remember like that this guy TD and from this parent to another one child is there simple okay so we both are cousins a and inputs are cousins like that so if you run this program guy so see it will immediately it will click on that particular checkbox okay so let's run it entering into the application and clicking on contacts and c-h clicking on there's two tests to check box with a single shortage clicking on it so no need to take care of that okay you will row by row and you know column by column you will nitrate everything and you will do such kind of two for loops drama and everything no new to do that lets you tomorrow you want to enter you said it's a checkbox for UI UI also so simple i'll write the same code for UI UI also and just need to change instead of this test to test to I'll pass this thing sorry UI here copy and paste here this is for test to test to by checkbox and this is why UI UI text box and then if you run it and see the magic okay so it's entering into the application from homepage - it will click on contacts and see simple within seconds it's clicking on both the checkboxes no need to take care of no need to handle those borings - for loops and then you will stuck into that you know where table concept or total number of rows you will find total number of columns you will find then you will iterate and once the test to test to match then you will have to click on this so to avoid this thing this is something amazing you can do very smart work you can do like this with the help of expert race if you are good in expert you can do some amazing things in expertise okay so peace practice spatially with the Google Chrome forget about Firebug and Firefox now always practice on this thing okay on this particular okay wizard in this particular field you can examine your expert and once you get one off one then the same expert you can check it over there otherwise what will you do you will write your expert over here and you will see that oh this expert is wrong again you will see okay what is the property okay in Firebug and prior path you change some property again you will run your code then it will show you on the console that okay XPath is not correct invalid XPath element not found or something like that so better you first check it over there and once you get one of one then you are good to go you can use simple this particular expert directly over here okay so whenever you are creating any XPath first check over here okay how to get this wizard I will repeat once again simple right click on any element and sorry right-click over here and click on inspect okay and just press control f4 magma guys press command F and here this is the thing you can try to imagine okay so race that's all for today please practice with you know this is a very small session I have taken not how to design your own customized expert on and check and examine and test your expert on Google Chrome okay so if you are good in these things you can do anything in selenium race you will be the you know you believe the master in Khomeini that's all for today guys I think I'll see you in the next session then thanks bye bye
Info
Channel: Naveen AutomationLabs
Views: 464,421
Rating: 4.8837399 out of 5
Keywords: Dynamic XPath in Selenium using Different Approaches, Writing Dynamic XPath in Selenium WebDriver, handling dynamic elements in selenium webdriver, how to write xpath in selenium webdriver, dynamic xpath in selenium using different approaches, dynamic xpath expression, how to write relative xpath in selenium, how to write xpath manually in selenium webdriver, Selenium, WebDriver, Dynamic XPath, preceding-sibling, forward-sibling, How to Find XPath in Chrome browser for Selenium Webdriver
Id: 3uktjWgKrtI
Channel Id: undefined
Length: 39min 48sec (2388 seconds)
Published: Wed Sep 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.