isDisplayed(), isSelected(), isEnabled() methods in Selenium

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is supriya i'm back again with another video on selenium webdriver series and uh today we will be learning about uh three methods in selenium webdriver those three methods are is enabled is disabled or is displayed okay so let's start okay so i have this website booking.com and i'm here at car rentals module okay and first we will be learning about is displayed method now uh consider uh an example wherein you want to show that this button uh if it is displaying or not okay if it is available or not or if it is visible or not then you would need to use a sd displayed method to uh get that output okay and i'll just open the eclipse and show you the code for that so i have this project selenium sessions and this is my package selenium class and inside this package i'll create one class okay so let me just uh name this class as visibility demo so let me just name it as visibility demo okay so this is my class uh i'll select main method and say finish now what i'll do i'll just copy uh my code from launching the browser to load page implicitly waits deleting the cookies maximizing the window and the driver.geturl method till here i'll copy and paste it in this new class visibility demo okay so guys i'll not tell you again and again how to launch the browser okay so if you want to know it again just watch my previous videos on how to launch the uh browser and what are implicitly weights and some of the window methods okay so here since my website for automation is booking.com and this module car rentals i'll directly copy this url from here to here and paste it over here okay so this will open my website booking.com and it will directly lead me to car rentals okay now if i want to uh show that this button register or sign in button is available or not or visible or not okay then i will use is displayed method okay so any element on the web page if you want to just check if that element is visible or not or available or not then use is is displayed method okay so let me just write the code uh before that let me just locate this element register uh button okay so i'll do an inspect now this is my highlighted section on html dom and let me see what locator i can choose uh for this i have this text and this is my tag span this is the uh class name if there is another class name with the same name or with the same value then we cannot use that so let me just create an x bar okay i'll just directly create the x5 and how do i do that i'll do control uh f and uh i have this search window on so i'll just write my expert so i'll say double slash okay and what is my tag name my tag name is span so i'll say double slash span okay and i'll have the opening and closing square brackets inside that i'll say contains this is a method or a function and inside this function i'll use the text function okay and since i'm using contains as a function so i'll put comma and my value of text is it's a register okay so i'll say register here okay so see uh i got my element highlighted here if i just mouse over here you can see that the button got uh highlighted on the top and also this got in this section got highlighted in yellow and here you can see it's it became one of one okay so i have my final customized x path for this register uh button and i'll just copy this and paste it over here so before that i'll i would need to write driver dot find element by dot this is the x path i'm using so i'll say bye dot x bar and i'll paste the value of the x bar and i want to check that if this register button is available or visible on the page or not so i use this method is displayed okay now this is displayed method will be returning boolean type of value okay so i just uh save this value in the boolean variable okay so let's say if it is boolean val is equal to uh the whole code so this method will return me boolean uh data or boolean value which will be stored in this variable and i want to check if it is true or false because boolean value is always true or false so i will just say system dot out dot println well and let's check what's the value so let me just run this code so currently it is visible so it will uh return me true on my console so it opened the chrome browser it entered the website okay and it might have found that the register button is available so here i go i have this value as true okay so is displayed method uh will tell you or give you the visibility of any element on the web page now let's learn another method which is is selected method okay now is selected method is used to check if any checkbox or radio button or any drop down okay these elements are selected or not so if it is selected it will say true if it is not selected then it will say false okay and in is displayed method also if uh selenium would would not been able to find out the element then it would have displayed the result as false but since it was there so it displayed it as true so let's go and learn uh is selected method so i'll just inspect the element first let me just close this window okay so which element should i select okay let me just scroll down i have this check box over here so currently it's not selected so let's see if uh it gives us the output as false okay so let me just inspect this element okay and i have the input tag for it for this checkbox and my name value is there so i'll just copy this and this time my locator is name so i'm saying driver dot find element by dot name okay guys if you did not understand how to locate the element please watch my previous videos and then you will understand everything how to locate the element how to use driver.fine element everything you will learn from those videos so uh i'll just paste the value the name value and i now this time we are learning is is selected method so i'll say is selected so you want to check if that check box is selected or not okay so i'll say is selected and this is again returning me the boolean value so if you mouse over any method you will come to know what value what is the return type so the return type is boolean you can see here so if you want to store the return value then you will say boolean okay let's say boolean val 1 equals uh driver.find element so i'll print my val 1 value so let's see what store what is stored in this variable val 1 so what what output we are expecting is since this check box is not selected it should display me false okay so let me just run this so for this it will say again true i'm not uh commenting this part for this uh register button and for this check box it will display me the false value so let's run this code okay so it launched my browser it opened my website and it should scroll me down okay since i'm not clicking it won't scroll me down but it it might have displayed the results let's go to eclipse and here i go so you can see for that button it's true and for this check box it's fall now what if i will select that check box and see if this turns out to be true so immediately after this code i will just copy this port from here okay and i'll just remove this and i'll say dot click okay so it will click the check box and it will get selected and this time i'm not storing uh this is click so it won't return return any type so it's void okay so it will click and then again i will check if this is uh getting selected or not okay so i have copied that same code here and i'll change this variable as the val2 and i'll make the changes here also weld okay so let me just save my work and check so what is my expected output true for uh this button is displayed method okay and is selected first it will find out that that checkbox is not selected let me just go to that website and show okay first it will uh check that this check box is not selected so it will say false and third i mean after selecting that okay it will click on this and then it will again check if this is selected or not so it should be true so it should be true the output should be true false true like that okay so let's run this code and check okay i have my browser opened maximize the window i have this website and see it scrolled me down it see my checkbox also got clicked can you see here and let's see the output so see true false true okay so now the third this is all about is selected method so if selected you will use for check boxes i mean these are the elements where you would need to select or deselect okay so all the elements where you are selecting or deselecting you will use this is selected method to check if that particular element is selected or not okay now another method the third method that we'll discuss is is enabled okay so that's very simple so i'll just pick up any uh button okay so let's pick up this button okay and we will check if this button is enabled or not so i'll just do a right click quickly and inspect this element i have this span tag and my uh okay why don't i use another locator if i have the id or something let's see okay so let's take this class name okay so i'll just use this class name locator this time and let me just go to my eclipse and see if this is working so what i'll do i just comment this part okay from here to here i'm commenting everything we will only check if that search but button is enabled or not so i'll say driver dot find element by dot this time i'm using class name so i'll say class name as the locator i'll enter that class name value and i'll check if that search button is enabled or not so i'll select this method and this is enabled but a method is again returning me the boolean value boolean return type so i'll just store this in a variable say val 3 and i'll print that on the console okay so let me just run this so the output should be true because that search button is enabled it launched my browser maximize the window it entered the url open the website and uh let's just check our eclipse okay it's still running okay let's see okay it's still running okay okay there is an error so what's what is that error let's see uh okay it's say it's saying that no such element unable to locate the element uh and which so selenium was not able to find with class name so let's create the x path okay see guys this is how you know you would need to check the error also it's it's equally important to check the error or exceptions okay and it's then only you will realize what mistake you are doing and you will rectify that error so selenium is all about working on negative side also and the more you get into the errors and uh finding out the solution more you will become strong in selenium okay so let's create the x path and how do we do that so what i'll do i will uh use the uh text method function only contains and text okay or i also have this attribute type attribute so let me just create the x path as a double slash button okay and square brackets inside the square brackets and write at the rate type equals the value is submit okay and what was my class name equals okay there's something wrong just a moment my attribute is submit it's correct okay i didn't close this inside the single quote okay so here i go so i have one of one here this is this became in yellow and when you mouse over this highlighted section you have your search button highlighted on top okay so i'll just copy this and this time this should work so this is my x path [Music] so i just make some changes here i'll say expat and i'll just paste the value of customized expat and see if this is working or not so guys sometimes your locators uh especially the class name locator does not work so at that time you need to create the xbox or select any other uh locator or attribute okay so what we are doing here is uh we are checking if search button is enabled or not so it should just give me the result as true because it is enabled so let's check so it's true okay now if this would have been disabled okay this the output would have been false right so this is all about is displayed is selected and is enabled method it was so simple i think the simplest part in selenium webdriver and up i end up with this session see you in the next video till then you have a great day stay safe stay healthy good bye
Info
Channel: Supriya Mishra
Views: 52
Rating: undefined out of 5
Keywords:
Id: XjZgZ6bCSis
Channel Id: undefined
Length: 22min 52sec (1372 seconds)
Published: Thu May 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.