Dynamic XPath using Different Approaches And FindElements Concept - Selenium WebDriver Session 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so how to create customized XPath so let's see we have on this side half dot ebay.com okay after DB calm is okay it's an e-commerce site okay they sell number of things they have different categories like books music movies and games and other systems so let's see I want to fill something here in this particular search field okay I want to write something here let's see I want to write Java and I want to select all category rice books and then I click on Go button so if I select if I have to enter something here in this particular text field so let's spy this so if you spy this particular text field you will see that this expert is the absolute expert okay this XPath is not correct so what happens is that okay because ID is not available for this particular input field ID attribute is not available so obviously I cannot use ID I can use name but let's see name is also not available sometimes then I cannot use maximum length or autocomplete or type right and then I should not use this particular XPath also although your script will work if you are using biot XPath and copy this XPath and not send keys if you are using it will work but ideally we should not use this expert why because this is called absolute expert now what do you mean by absolute XPath remember I told you at a time of locators that absolute expert means this is a complete full address of that particular element from baton node to child node so if you see that first they have written at the rate ID is equal to header search bar okay so it means there is a one form is there where ID is equal to hetero search bar then they have one div then div then dev number two then tip number two in their dev number two they have table then table then they have T body the body then TR and TD number two TD means second column so TD 1 and T 2 and inside this TV - they have input field so this is a risky s XPath why let's see tomorrow the search will shifted from here to here from left to right so obviously this TD will become five or six this div will be changed right let's see this search field has been shifted from here - okay little bit down there in that case again dev - will become deaf 5 or dip 3 or DIF 6 whatever right so we should not use this absolute expert right if in between any path is getting changed or any attribute or any element is getting change or any node is getting change then your ex back will be disturbed so you should not use these kind of x month okay so what is the solution the solution is so let's create a class and my class name is let's see custom X path okay select the main method and click on finish you will do hear me right is now in one question like oh how will we come to know that it's not a valid expert every time this is this is coming like this right if you spy this books attribute I mean books element then this is not absolute expert it's clearly written at the rate I D equal to M so this I can use that it's not coming in the form of hierarchy that / Dave / dr / t bali table or something like that but for some field where ID is not available or some other attributes are not available in that case it will give you this particular expert we should not use this this is hierarchy based expert can you see that /div /div - table before you know we should know it should have ID in that input below also it should have ID right yeah if ID is available then it will give you at the rate ID will give you relative expert the proper expert ok don't keep but sometime it is not compulsory that ID is always there he's right so we should not use this particular expert now then what is a solution so in currently in this case I can use bio dot name so let's see name is also not available sometimes right let's a name is also not available then we cannot use this also we don't have ID also and we don't have name also then in that case we have to create our own expert so how to create so first okay so let's take this thing then copy dari it all cookies page no time out and everything remember yesterday I explained you all these things so this is a good practice to define all these things and then half dot ebay.com okay so we do one thing that's launch hard what will become immediately after that okay now what we have to do we have to enter something here in this particular text field so I'll use customized Xbox the simple driver dot find element by dot expert ok and obviously X but I have to use but I have to write my own X but now so how to write so always remember XPath should start with double slash so I will put double slash like this okay and then first I will check what exactly I will spy that particular element and what is the HTML tag the HTML tag is input right so I will try to input over here okay input and then I will write these two brackets okay square brackets and after that I check what are different properties are available so property one property is there that is class property good so I'll use class property how to use class property like this at the rate class okay is equal to and in single quotes we have to give that class property value the value is this AC textbox so I'll write AC textbox right this will become my customized expert I'll repeat it's very simple put first of all double slash because we know that it should start with X Y should start with double slash then what is the name of that particular what is the HTML tag of that particular element so tag is input tag and then what exactly the attribute you are going to use so let's yeah class is available so I'm going to use attribute with at the rate so at the rate class is equal to AC textbox so at the rate class is equal to AC textbox and that's it and now what exactly want to enter same concepts and keys and send give is ready I want to enter Java right so let's see this particular I have created my own expert so this particular like with XPath okay it's working or not it's entering Java or not okay so let's run it so if you see Google Chrome is getting launched see it's entering Java right so my expert is absolutely working fine okay so there is one more way of creating the customized XPath let's see I don't want to use I want to use some other attribute instead of class so simple I'll write like this again driver dot find element by dot XPath I know that a double slash what is the input what is HTML tag input okay and then I can use of other property also I can use name property also let's see name is available and I don't want to use by dot name so you can use name property name is query so you can use like this at the rate name is equal to make sure you are writing the value in single quotes single quotes this is the name and that's it dot sendkeys lets you embed in java so either you can use class or you can use name also at the rate name like this okay so let's see I am commenting this line I am trying with at the rate name okay so let's run it okay so with name or so it's entering Java so we can use at the rate name also okay now we have one more way to do that driver dot find element by dot XPath and we know that X bar should start with double / - a double slash what is a HTML tag HTML tag is input input square bracket square bracket then there is a function in XPath library that function name is called a contains okay so we can use contains like this and contains the functions we should write bracket like this contains what it contains class okay so it contains at the rate class make sure if you're using contains you should not use equal to like this you should use comma C you have to remember okay these indexes if you're using contains you have to use for the attribute class comma instead of equal to okay you have to use comma and what exactly the value of class the class value is AC textbox h3 textbox and dot send keys and send key this Java okay so this is contains function input which contains what it contains class the class value is AC text box so you can use contains is very powerful so you can use this particular thing holds some time what happens so first let's see this is working or not with contains so let's run it it's launching the URL can see its end Java over here so with contains also XPath contains also it is working so you can use contains also now when to use contains she contains is very important 3 sometime what happens let's see ID is available ok so they have given ID is equal to let's see test underscore one two three okay this is the ID so you will say that okay Naveen I use by dot ID and this is my ID test underscore one two three you can use by dot ID and then use test render square one two three ID something like this but if you refresh the page or if you run the script again now the next time ID is coming like this ID is equal to test underscore four five six then it will be a problem or you have written here a script by Road ID test in the square one two three but next time if you are executing your test cases okay on the UI ID is for that particular element is becoming now test underscore four five six for the same element okay if you close the browser and open it again now the ID becomes ID is equal to test and the square let's see seven eight nine so every time ID is so dynamic in nature that sometime it's giving a test underscore one two three sometime it's giving test underscore four five six sometime it's giving test underscore seven eight nine so after underscore some number is okay getting change everything right so obviously you are not going to change your script we have written out a script bit test in the score one two three so you are not going to change your scrip again and again right so what is the solution for that so if the ID is coming like that this is quite dynamic ID okay this is a concept of dynamic ID the ID is not constant every time with getting changed okay every every time when you refresh the page or you load the page or you open the page again every time ID is getting changed right so how to solve this problem to solve this problem we have contains so what we have to you see let's see if I want to use this particular ID attribute so simple driver and dot find element by dot I'll use XPath okay fine and I know that let's see this is input field okay what is the HTML tag input tag and I will write double slash input okay contains okay contagious a function I'm going to use at the rate ID comma and use only test underscore that's it okay and let's see whatever you want to fill sendkeys I want to enter send key let's retest okay so you can use like this contains it can be used like input contains at the rate ID it means find a particular input which contains ID is like this test underscore okay so it will ignore that dynamic part one two three or four five six or seven eight nine it will ignore this particular problem okay so we can use contains like this is to handle the dynamic IDs like this and it's a very famous individuation raise how will you handle dynamic IDs they will give you example like okay some time ID is testing let's go one two three next time it will be test underscore some other number or tetanus four four five six or seven eight nine so how will you handle it right base please be on mute so if you want to handle you have to use contains function now apart from contains we have one more very important function to handle the dynamic ID we can use driver dot find element by dot XPath an expert I know that starting with double slash I'll try it input okay make sure guys you are not doing any spelling mistake and put square bracket we have one more method that method is called starts with starts with is a method okay starts with what start with at the rate ID comma test underscore this is again very important will be new send keys and Latisse and give this test ok now see what is the starts with the start just like contains we have start contains means a particular input a particular ID contains test underscore starts with means this is a again another function starts with it means a ID an ID is there which is starting with test underscore so this is more appropriate okay to handle these kind of scenarios so we have to find out one input field where the ID is starting with test underscore and whatever the the after underscore whatever is coming like 1 2 3 4 5 6 or any random number we don't care about it ok so those things will be ignored so we can use either of them ok either it contains this or it contains or starting with starting me in this first letter is starting with the first word starting with test underscore like this sometime you are getting ID like this let's see ID is equal to some very long idhe test underscore test underscore 7 8 9 underscore something like this this is very weird ID right and every time this number is getting changed okay next time it will be let's see 1 2 3 4 so you can use like this input starts with this test underscore test underscore like this you can use so you can ignore that particular part right sometime you getting let see I D is equal to test or maybe something like this one two three four underscore test underscore T okay and if you refresh the page again I D will become like this two three four five six underscore test underscore T next time if you write like this it's coming like this six seven eight nine underscore test underscore T so we know that there is a pattern the pattern is every time is starting with some number and then this part is constant underscore test underscore T again some number and it's the pattern is constant underscore test underscore underscore T right so here in this case we cannot use it starts with okay in this in these three cases we cannot use it starts with this if you are using a start with something like this underscore test underscore T it will not work because it's not starting with underscore test right so either we can use contains or we can use one more method that is driver dot find element by dot XPath and we know that Expo should start with double slash we will write input and we have one method ends with okay ends with what ends with at the rate ID comma we have to check a particular ID which is ending with underscore test underscore people right and you it's similar to the string functions which we use right we use that string format yeah many times I'll be very pleased we are giving one function that find and particular ID which is starting with test underscore find a particular ID ID which is ending with underscore test underscore something like that so we are first we are checking what exactly the pattern of that particular element okay and then on the basis of that we will write we will choose contains or starts with or ends with okay so these three very powerful functions okay methods are available in XPath most of the time we will be using okay XPath contains function okay so these three examples of I would say here this is a good practice to use the starts with okay this is Maurice or to start with this is for ends with right this is for starts with this is for ends with okay so we know that is the pattern is test underscore test underscore test underscore is starting with some common name so this will become starts with we have to use it starts with and we know that it's ending with test underscore test underscore test romesco T so we can use ends with okay so like that you have to decide but it's not the case always you have to you have to use but this is very powerful so although these fields are not available in half body becomes I am just commenting these three lines okay but make sure this is very important now I will give you one more example let's see I have to click on my account so I will spy this okay so I can not use again this expert again it's coming in the hierarchy form /t body / TR / TD 1 / a number of slashes are there so I want to use it right and there is no idea also there is no name nothing is there so we have only link text so I can use link text my account link text is available right but I don't want to use my dot link text also so in that case how to create your own expert for clicking on a link Spacely poor link I want to click on my account driver dot find element see everything is one about expert we're discussing everything all about expert today okay how to create different types of expert for lengths for fields another field right so by dot expert and I know that X bar should start with double slash first I'll check what is the HTML tag what is the HTML tag HTML tag is I'll spy this my account link an HTML tag is a okay and see always remember all the links are represented by a HTML tag okay so I'll write this particular note it represented by a tag I would say a HTML tag okay so whenever you see any link the HTML tag will be a-ok for links so I know that exposure should start with double slash so I will write a right and then square bracket okay and then I'll use contains function so a contains what contains the function so I will use these two a small bracket because this is a function and a contains what a contains we have another method inside the contains we can use text function again it is a function okay see you don't worry about the syntax you have to practice then only you will get to know about the syntax okay so I will repeat a contains contains what text and what is a text text is comma this is a text my account M capital a capital my account is a text and dot we have to this is a link so we have to click on it not click so this is again customized XPath see I'll repeat double slash a because a is the HTML tag and a square bracket and I will use contains in contains what exactly the two parameters we have to pass this is what is a text text is again a function okay so you don't use at the rate text use text as a function text comma what what exactly detects it carries the text is my account and then not literal okay so let's see it is working or not it's clicking on my account or not attempting Java and then H clicking on my account see it coming on this page my accounting right so this is how we use customized X path for links right so let's see I want to use know for all the links it will work let's see for sell my stuff so I know that okay sell my stuff is there so I either I can use by dot link text see it's not compulsory to use customized XPath always I am just giving you an example if you want to use customized XPath customized expose our very powerful are very fast as compared to absolute XPath okay so this is called absolute expert see guys we should not use these kind of expert this is called absolute expert now and see the same expert this is called absolute XPath absolute expert now see the expert difference this is XPath Dave Dave number two table ok so then one form field is there where ID is equal to this this is a parent okay know what and then we have another Dave's def section under that that section we have set go to second div and then go to the table then T body then TR then second column TD - and then input field is available let's see in between if the developer they remove this Patil relative to your expert is gone right you won't be able to debug what is a problem then again you have to change the expert so that is a big problem we should avoid the changes in a script in the form of X paths or locators none so we should make it very gender ik whenever you are creating any script or any framework make sure that you are creating your script very general minimal changes you have to do in future in your script so you have to write a very reliable and the robust expert and script so that Jen Trippi you should write okay you should not use these kind of expert this is totally not recommended okay so absolute XPath not not recommended sometimes we you have to use actual ooh 2x but we don't have any choice in that case 0.1% chances are there that we have to use absolute X but we should avoid to use absolute expert although it will work today it will work but there is no guarantee that tomorrow it will work or not okay now you can see the difference this is called a customized expert okay this is called a customized expert so we should always use customized expert now what is a major difference another difference is performance of this script this expert will be very slow first it will go to this particular period node then it will check Dave then div - then table then T body and TR then second TD then input then it will try to enter a Java okay so let's see the similar kind of expert you are using 100 times okay so your script will become very slow so we should avoid these absolute XPath here the advantage is it will be very fast because it will immediately attack not okay wait what exactly I have to find one input field which contains at the rich class which is is he textbox so immediately it will try to enter the java whatever the value you are passing okay so we should not use absolute expert so what are the disadvantages of X pollute XPath the first disadvantage is performance issue second is not reliable okay third is can be change at any time in future I'm just writing simple notes so that you can prepare for interview okay it can be changed at any time okay in future okay so obviously we should not use this we should use customized XPath so what are the advantages of customized expert the customized ad advantage is that very fast syntax is very simple okay the performance issues are not there and it's unique its generic okay same thing to handle the dynamic IDs also or dynamic you know properties are there in that case we can use these three functions contains starts with and ends with what exactly the pattern on the basis of pattern we have to use that for links also we can use text function for links also we can use text function like that okay any doubt okay so this is called customized expert so you can pick any example and you can practice with customized XPath okay so one more topic I am going to cover today very important topic what is a driver dot find elements okay so let's see if I ask you one interview question that this half dot ebay.com okay on this particular page how will you count how will you get to know that okay how many links are available on this page give me the count of total links using selenium script give me the total number of links how many links are available or give me the total number of links as well as give me the text of all the links how will you do that because there are so many links see there's so many links are available right there are all these are links okay I have to give them see all these footer links are available okay there are number of links so how will you get to know that how many links are available on this particular page and give me the text of each and every link so how will you do that it's a very very famous interview question in selenium and sometimes you have to use in your actual projects also so how to do that so let's do it okay so I'll use this thing copy this entire basic stuff some initial part right and okay so I have launched Habiba comm so hard on ebay.com I have launched and now what I have to do I have to my target is get the count of links on the page this is my first target and second thing is get the text of each link on the page so these are my two targets so how to do that first get the total count I would write total go get the total count of links on the page so we have one method driver dot find elements not find element find elements okay we have to use driver dot find elements okay because we have to deal with number of elements now so driver dot find elements by dot what by dot we have one method tag name by dot tag name and we know that tag name is what HTML tag name and we know that if you want to find out links total number of links and that is we know that all the links should start with a smile right tag name a okay so we had this method driver dot find elements by dot tag name a and semicolon so with driver dot find elements we don't use any dot click or dot send Keys nothing okay so driver dot find elements this is the method okay by using what by using tag name and we know that all the links are represented by a tag so I use a right so this particular method see if you mouse over this particular method will give you one list a list of web element so in the left inside what I'll write list of web element and let's see the object name I'm creating the cheap link list okay I'll repeat don't be confused driver not find elements by dog tag name is a method and a is HTML tag why I am writing a here because I have to find all the links and I know that links are represented by a tag remember yeah all the links are represented by a tied right we know that all the links are represented by a HTML tag so obviously I'll use by it or tag name a so what exactly it will do it it's same driver please give me all the elements which are having tag name is equal to a right and this driver dot find element will give you all the links in this particular linked list object and what kind of object it is it is this type of object which is having all the web element okay so this is a syntax you have to remember okay so list is available in Java so we will import it from Java dot util web element obviously it's a class available in selenium so we will import it from selenium so whenever you are using driver dot find elements okay then in the left hand side you have to use list of web element because the written type of dry find elements will give you a list of element okay and just create the any object name let's say I'm giving object name linked list you can give any name list or links or ABCD or whatever the name you want to give okay so this list object will be having all the counts all the links in this particular object now simple remember you can refer my you know session for error lists you can refer those that particular session for analyst this is same error list so if you want to get the size of ArrayList so now how to get the size of linked list the size of linked lists let's say I want to print the system dot out dot println linked list dot size system dot out dot println linked list dot size so all the number of ok all the a tags will be stored in this particularly linked list and then I'm giving linked list dot size so it will give you total number of links are available on that particular page so if you run this page let's run it executable oh we haven't copied that part system dot read property ok now let's run this pool okay so it's launching half-body be calm and then it will give you the total number of links are there 185 total links are there ok so guys it's very if I must enter be patient they will ask you how many links are there how many buttons are there how many input fields are there let's see if you want to count how many input fields are there you will write input over here we know that all the input fields are represented by input HTML tag right if you want to see that how many buttons are there so we know that ok sometimes buttons are starting with button in HTML tag how many it will give you a total number of buttons on this particular page okay but most a times they will ask you how many links are there so we know that links are starting with a HTML tag and we are using byte or tag name right and that we are printing the size link list dot size so there are 185 fields are there 185 links are there okay 185 fields links are there on this particular page some links are visible some links are hidden on the page but total number of links are 185 now the first target is completed total number of counts now get the text of each link available on the page so how do i iterate any error list remember array or analyst how to print all the values of array or error list we have to use for loop right so to print all the text and everything we have to use for loop so I let's start where for loop for n I is equal to 0 I know that index will start from 0 in list object and then I less than the size of that particular linked list dot size and then I plus plus write I equal to 0 I less than what is the size size is 185 so it will my loop will start from 0 to up to 185 and then I plus plus so instead of writing 185 directly I would use dot size method directly linked list dot size right better to use dot size bigger tomorrow a cheap 4 or 5 more links are available then the size will be will be 190 okay so I equal to 0 length will not rise in I plus plus now what I'll do I have to print the value of each and every text so what I will do we have this method first I know that linked list dot get method is there so we know that how to get the value from array or okay from aerialist remember dot get method to add the values we have dot add method to get the values we have dot get method so don't get I so first time I will be zero so I equal to zero linked list dot get zero so it will point to that particular first link ok first link available in the object so there is an object where 185 links are there so we will starting from the loop I equal to 0 get 0 okay and from that particular now it will point to the first link now what exactly we want from that link we want give me the text what is your text name so we have one method and get text name nothing is there okay linked list okay this is the object name dot get ie why I am using it because we know that for list object we have to use get method to get the respective values okay the linked list dot get I is equal to 0 I equal to first n 0 so it will point to the first link out of 185 links and then give me the text so dot get text now I know that dot get touch will give you on a string so I'll store in some string variable let's say string link text is equal to this right and then I'll print it system wrote or print and I will print link text okay so whatever takes available it will give you it will give this particular text in this very where I'm printing it next time I equal to one now it's pointing to second link get one and give me the text so let's see the first element is this first link is this it will give you my account second is wishlist okay so this is 0 this is 1 this is 2 3 4 5 and let's see if any other links are there then this is 6 7 8 like this 9 10 11 or whatever okay like this it will count one by one and then get the text for each and every increment so let's run it and then we'll see what exactly it's pending I'm maximizing my console and we'll see and see it will start printing 180 is now 180 several links are there and see it's printing all the text you can see that on the run zone alright so earlier it was 185 now see immediately it becomes 187 so you never know that some time number of links are getting increase or dropped right so 187 links are there but you will see some blank it's printing some blank numbers why so there are some links see there are some links which are it doesn't have any text those are blank links okay you won't be able to see on this page but developers they have written some a tag they have created some HTML tag which is equivalent to a but they don't have any text so but it's still pending its printing blank text out of 187 some fields are there some links are there which are having blank text okay now then it will start the actual thing that my account of wish lists sell my stuff help see my account will a cell must of help ebay home and sign-in ebay home okay and then sign in then register and then all the book names how to read literature like a professor so starting from something or no from where is it starting where is that book name how to read so okay maybe somewhere in the background it's there and then okay and fifty shades of trilogy and in York and george RR martin and hunger games and all so all these coming from here okay there is no sequence but it will print all the things from here see from here it's printing this is also a link how to read literature like a professor fifty Shades trilogy George Arthur Martin see it's printing all the values over here george RR martin you know okay and then it will print some footer links you can see that user agreement privacy policy privacy policy announcement why ebay show ups games each printing from here shops why ebay announcement right it's printing of home books textbooks music printing home books textbook music movies games shops movie music movies games right so all the links okay it's printing on the console there are some blank links which doesn't have any text in that case it will give you the blank output blind okay so there are total number of a one eighty seven links are available and see this will work for any website you can open facebook amor open any any other site okay so let's see I'm trying with facebook.com okay so face it will open Facebook and then it will find out how many links are available on the Facebook first page and then let's run it so it's opening Google Chrome and during facebook.com and see it's giving total number of links on see total number of links are available on Facebook 54 links are there on the Facebook home page and it's printing all the links so see you will see Facebook forgotten past account why do I need to provide my date of birth see why do I need to do my date of birth okay forgotten account all the links create page and see all these links all these are links English Marathi in the blah blah blah Punjabi and all those stuff okay all the links are coming see all the language links are coming over here okay all the footer links are coming over here from sign up messenger Facebook Lite Mobile sign in the blog in messenger failed to client mobile all the links are yeah so total number of links on Facebook 54 links are there right so it's a very very famous interview question very important concept also how to use biotech name with driver dot find elements method is there okay so the second target is also achieved that get the text of each link on the page first total number of links second thing is get the text of each link on the page that's it very simple okay so that's all for today any doubts
Info
Channel: Naveen AutomationLabs
Views: 177,263
Rating: 4.8850989 out of 5
Keywords: Java, Selenium, TestNG, WebDriver, Test Automation, Automation Framework, handling dynamic elements in selenium webdriver, how to write xpath in selenium webdriver, dynamic xpath in selenium using different approaches, how to write relative xpath in selenium, how to write xpath manually in selenium webdriver, absolute xpath in selenium, how to write absolute xpath in selenium, selenium xpath java
Id: LywLnI7hHhw
Channel Id: undefined
Length: 49min 38sec (2978 seconds)
Published: Wed Aug 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.