Explicit Wait in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome back to learn - Automation dot-com this is Mukesh today in this video we'll talk about explicit weight in selenium-webdriver so till now we have not discussed any weight in our YouTube web series so today we will start with explicit weight so let's see the high-level agenda for today we'll talk about what is weight in selenium-webdriver what is explicit weight we'll see a syntax how to write code for explicit weight and we'll see a simple program how we can use it and I have written one article on this so for the complete program syntax and theory you can check this link so let me show you an example of for explicit weight so you can visit this URL for the practice I have a timer here so right now you can see we have a web driver right but if i refresh this page by default you will not have anything here so once I start the timer after a certain interval you will get that webdriver keyword so my requirement is I need to wait until that webdriver keyword is present or its until it's not coming on the you I keep waiting so if you see the page source of this let me search for webdriver so you can see here we have three key words here selenium qtp and webdriver and this is just a small JavaScript you don't have to understand this because this I have taken from w3 school so what this does when I click on this button that button after of two seconds of interval it will fire a specific keyword so after two seconds it will fire selenium after four second qtp and webdriver so we need to wait until this ki webdriver is not coming keep waiting so this is kind of dynamic wait so here we cannot use implicitly because implicit wait only will check on the page labeled or you can see the dome level but we need to check until it is not coming on the UI keep waiting so in this kind of scenarios you can use explicit weight where you have to wait for a specific condition so this is just one example you will find multiple scenarios I will show you once we start so I will show you once I click on this click me to start timer you will notice after certain time this will go and it will start selenium qtp and webdriver so we have to wait until this webdriver is not present until this webdriver is not coming keep waiting so we have to write one wait for this so let me show you first of all let me write one XPath for this so I will start Firefox because I have that plug-in in Firefox only so let me start the timer and I will write the expert for that web driver so the XPath which I will write I will say search for a specific paragraph where text equal to webdriver right so this XPath we will use in our application or in our script so let me create a separate class and I will give this class name as demo I will take public static void main I will quickly write the code to start Firefox or Chrome so let's use Chrome because chrome is faster than Firefox let me maximize it let me pass the URL s well so the URL which we will use is this and let us try to find out that element so I will use this and this will only come once we click on this timer button right then only this timer will start so I will take this XPath so you can see this expert is weird here ID contain some dis so let me use this text so if you are not familiar how to write dynamic XPath you can visit my previous videos where we have discussed how to write XPath so I am writing give me one button where text equal to this right so you can see we have written very good XPath so let us copy this and I will click on this so same thing they would find element by XPath and dot click just give a tension here I am not using any weight as of now so ideally this program should fail because after this statement it will immediate try to find this very hour which is not available so it will fail my script let me write here so once this element comes I will check is displayed if yes never right from condition if status equal to true then definitely it will print like element is displayed I have written the same code in that URL which I just shared so you can copy paste the code and check I mean element is not displayed so if we run this code you will notice after this click it will try to find this web driver which is not available so it will fail the script from this statement itself and it will not proceed further so let me right click run as java application so it will start the crow chicken seat it clicked on the timer and this timer is started but our script failed and as expected it just started looking for that web element which is not present so you will get no such element exception because it is not waiting so let's wait let us keep one implicit wait as well and let's see how it works so I will put driver dot manage dot Windows not Windows it's time out dot implicit wait let's wait for 30 seconds and let's run the script again I will close this previous window and you can see same thing we have done using implicit weight implicit weight is checking until that element is not coming keep waiting but element or sorry this implicit weight will be only applicable for this visibility and display conditions when you have more than explicit sorry when you have more than visibility conditions then you have to move to the explicit weight I will show you some of the conditions where this implicit weight will be limited so as of now you can see previously we have not used this implicit weight so it was failing but now we use implicit weight so it is waiting until this element is not visible on the page right so now we give in 30 seconds so it is waiting and it is printing element is displayed now what if I remove this I need to wait for a specific condition so we can write a small code here we can write webdriver wait wait so just check for this webdriver wait if you put mouse over here one minute let me attach the source right now we are not getting so so let me attach a source no problem let me download the source code generally I prefer downloading code because it will help you in the long run what exactly each method will do so let's go to this download page and go to the previous releases here I have older version so I will go and download selenium Java to 53.1 zip let me ins if this so this is the source file we have so let me attach if I go to tech stop selenium source code yes so now it will give you some additional information about that specific class so to start with this explicit weight we have be used when webdriver weight class so you can see it's a specialized form of fluent weight that is webdriver instance so what is fluent weight I will show in the next video so as of now we'll create object of this class so webdriver weight so what this constructor will ask it will ask you one driver and timeout in seconds so we can give timeout in seconds say 30 seconds and we can use this weight object and we can call one method call until so we are giving wait until the expected condition is not true so you can write your own expected conditions but already selenium has some expected condition so they have one class call expected conditions if you put dot here you can see they have so many expected conditions which you can choose so implicit weight which we have selected previously that will only check the element presence and visibility but here you can see there are so many other options like wait till the text to be wait till the title contains this wait till the title exactly like this wait till the URL contains this wait till the visibility of element wait till the invisibility of element wait till the frame to be available and so on so whatever weight will give you so much flexibility so as of now this or this visibility of element this will help you to check until that element is not visible keep waiting so you can see the description says I'm just going to use this method visibility of element it says an expectation for checking that an element known to be present on the dome for a page is visible the good point about this is visibility means that the element is not only displayed but it also have some width and height that is greater than zero so what it will accept it will accept element as a parameter and it will return you web element once it is visible so you can see this description is very important if you are beginner in selenium so it will give you exactly what each method will help you so you can attach the source as I did and you will get all the conditions so as of now I will check visibility of element so you can provide this verb element or better will take feasibility of element located by XPath and you can provide this XPath sorry there's some mouse issue so there is one additional parenthesis yes yes so now we have given a condition here let me return it yes so now our program is little bit dynamic in nature so yes you can use implicit weight here but explicit weight will give you so many options to handle multiple situations so let's see how to use it we created object of webdriver weight and using this weight object I called one method called until and in the until method we have given some expected conditions and the condition is visibility of element so until this web element which is nothing but this is not present keep waiting and if it is coming within 30 seconds it will return you as a element then you can play around it but if it is not coming within 30 second then it will fail so let's give webdriver weight of 5 second just to check we know it comes after 6 seconds so definitely this program will fail so you will notice after a certain time it will fail right you can see it's failing because time out over and it gives a meaningful exception expected condition failed waiting for visibility of element located by this but within 5 seconds it is not coming so let us increase the timeout because we know it comes after 6 seconds so every change the value from five to ten and let me close the browser soon and it will click on this timer now the selenium qtp and webdriver and you will notice it will print you element is displayed right as simple as that so right now I have added only one condition but you can add multiple conditions here ok you can wait until the title is not coming and so on you have almost 7080 methods so I have documented everything in this URL so you can check the complete code yes this is the conditions when you can use it these are all the conditions which I just showed this the syntax and finally the code which I just written so just try the same code from your in let me know if any issue thank you so much have a nice day bye bye
Info
Channel: Mukesh otwani
Views: 78,314
Rating: 4.7506232 out of 5
Keywords: Explicit Wait in Selenium Webdriver, Selenium WebDriver, Test Automation
Id: rlUzo_w11Ao
Channel Id: undefined
Length: 18min 8sec (1088 seconds)
Published: Sun Sep 11 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.