Session 26 – Selenium with Java | WebDriver Methods — Transcript

Learn how to interact with web elements in Selenium using Java, focusing on WebDriver methods like get, conditional, browser, navigational, and wait commands.

Key Takeaways

  • Web elements are any interactive components on a web page that Selenium can automate.
  • Locating web elements is a prerequisite to performing any actions on them.
  • WebDriver provides several predefined methods categorized into get, conditional, browser, navigational, and wait methods.
  • Get methods are fundamental for retrieving information like page title, URL, page source, and window handles.
  • Window handles are unique IDs generated at runtime to manage multiple browser windows during automation.

Summary

  • Introduction to web elements in Selenium and their types such as input boxes, checkboxes, radio buttons, and dropdowns.
  • Explanation of the importance of locating web elements before interacting with them.
  • Overview of key WebDriver methods categorized into get methods, conditional methods, browser methods, navigational methods, and wait methods.
  • Detailed focus on get methods including getTitle, getCurrentURL, getPageSource, getWindowHandle, and getWindowHandles.
  • How to use the WebDriver instance to access these predefined methods.
  • Use cases for get methods such as opening URLs, retrieving page titles, validating URLs, and capturing page source.
  • Explanation of getWindowHandle and getWindowHandles for managing multiple browser windows with dynamic IDs.
  • Clarification that these methods are essential for day-to-day automation tasks in Selenium.
  • Emphasis on the dynamic nature of window handles and their runtime generation by the browser.
  • Practical insights into how these commands help in browser and web element interaction during automation.

Full Transcript — Download SRT & Markdown

00:03
Speaker A
Right, so far we have learned how to locate web elements in different ways and approaches, using basic attributes like ID, name, and then CSS and XPath, and so on. We have also seen different types of XPath and how to create relative XPaths, and so on. So from today, we are going to start how we can interact with different types of web elements.
00:25
Speaker A
today we are going to start how we can interact with the different type of web elements so what is a web element web element is an element which is present on the web page it can be input box
00:36
Speaker A
So what is a web element? A web element is an element which is present on the web page. It can be an input box, it can be a checkbox or radio button, dropdown, anything you can see on the web page, which is called a web element in the Selenium perspective. You can call it a web element, and in some automation tools, we can call them web objects. Web objects or web elements, anything is the same, but in Selenium, normally we call them web elements. Everything is a web element.
00:52
Speaker A
same but in selenium normally we can call them as a web elements everything is a web element okay so first of all let's see how we can handle this web elements so before handling handling is nothing but first we need to locate a web element and then we will perform certain type of an action on the
01:12
Speaker A
Okay, so first of all, let's see how we can handle these web elements. So before handling, handling is nothing but first we need to locate a web element, and then we will perform certain types of actions on the web element. Every web element has different types of actions we can perform. So before going to learn how to handle different types of web elements, there are a few commands in Selenium that we have to learn before interacting with the web elements.
01:30
Speaker A
types of uh commands we can say commands or web driver methods we can say methods also so these are all predefined methods which are already there in web driver and we should know how we can use those methods so let me list out those methods so get methods conditional methods browser methods
01:50
Speaker A
Okay, so there are five types of commands. We can say commands or WebDriver methods; we can say methods also. So these are all predefined methods which are already there in WebDriver, and we should know how we can use those methods. So let me list out those methods: get methods, conditional methods, browser methods, navigational methods, and then wait methods. So these are the five different categories of methods which are very commonly used in our automation, and these are not related to web elements actually, but yeah, we can use these conditional methods on web elements also. But there are some more other commands also. These are some basic commands which we have to use on a day-to-day basis in our automation.
02:09
Speaker A
other commands also there these are some basic commands which we have to use on day-to-day basis in our automation okay so first we'll start with uh these commands first get methods so there are some methods and all these methods we can access from the driver web driver object so as soon as
02:30
Speaker A
Okay, so first we'll start with these commands, first get methods. So there are some methods, and all these methods we can access from the driver WebDriver object. So as soon as you create WebDriver driver equal to ChromeDriver, by using the driver object, we can access all these methods.
02:50
Speaker A
how we can use them so get methods we can access these commands through web driver instance and uh why we call calling them as get method we can simply say all these command or methods starting from get keyword like get get title get current URL get pay Source get window handle get window
03:12
Speaker A
Okay, so first let us start with get commands. There are five to six commands under get commands. We'll see what those commands are, and then I'll show you practically how we can use them. So get methods, we can access these commands through the WebDriver instance, and why do we call them get methods? We can simply say all these commands or methods start with the get keyword, like get, getTitle, getCurrentURL, getPageSource, getWindowHandle, getWindowHandles. So these are the six commands or six methods in WebDriver.
03:26
Speaker A
on the browser so this we already seen many times so as if you want to launch any browser or open the URL application so this the command or method we have to use and get title sometimes we need to
03:40
Speaker A
And what is the use of these commands? So get is one method; when I pass a URL as a parameter, this will open the URL on the browser. So this we have already seen many times. So if you want to launch any browser or open the URL application, this is the command or method we have to use.
03:54
Speaker A
the title of the current web page using get title method so this will also Returns the title of the page in the string format same thing get current URL so while navigating between multiple Pages if you want to validate the URL is correct or not so you can capture the URL of the web page that
04:13
Speaker A
And getTitle, sometimes we need to perform some validations on the titles of the web page. So if you want to get the actual title of the web page, then we will compare it with our expected title. So in those cases, we can get the title of the current web page using the getTitle method. So this will also return the title of the page in string format.
04:33
Speaker A
response is uh we cannot understandable format so that is a combination of HTML JavaScript some CSS the combined code will be there so if you want to get that page source so we can use this command it will return the source code of the web page and for example let's say you can take
04:53
Speaker A
Same thing, getCurrentURL. So while navigating between multiple pages, if you want to validate the URL is correct or not, you can capture the URL of the web page that you can compare with your expected URL. So for that, we use getCurrentURL. So this method will return the URL of the web page.
05:05
Speaker A
view pay source so if you want to capture this information from the pay Source you can use get pay Source command the next one get window handle and get window handles so the get window handle method will returns an ID of the single browser window means so normally elements are having some
05:29
Speaker A
And getPageSource, so this will return the source code of the page. So whenever you browse some page, you will get some response from the server actually. That response is in an unreadable format. So that is a combination of HTML, JavaScript, some CSS; the combined code will be there. So if you want to get that page source, we can use this command. It will return the source code of the web page.
05:52
Speaker A
Dynamic every time you won't get a same ID okay and by using that window ID we will do certain type of operations like switching between multiple browser windows or if you want to perform actions on multiple browser windows if you want to achieve this we need this window ID so that window ID we
06:10
Speaker A
And for example, let's say you can take any web page. So I'm just browsing one page, but as a user, I'm able to see this Google form, but the server responded like this. You can see View Page Source. If I use this, this is your View Page Source. So if you want to capture this information from the page source, you can use getPageSource command.
06:23
Speaker A
that ID will be created by the browser itself at the run time and that ID will not be same if open google.com page now the ID will be different next time it will be different every time when you open the browse or when you refreshing the browser or when you open the application every
06:38
Speaker A
The next one, getWindowHandle and getWindowHandles. So the getWindowHandle method will return an ID of a single browser window. Normally, elements have some attributes. Suppose if you inspect this element, you can see attributes of the web elements in the DOM itself here. You can see. But every browser window, suppose I'm using this google.com, okay, and this browser is having some kind of an ID that will be generated at runtime, and that ID is also more dynamic. Every time you won't get the same ID.
07:02
Speaker A
application this let's say have open one application here so this window is having one ID by default and when you're trying to navigate some other window suppose I'm clicking some link and it is opening another tab or another browser window now currently I have a two browser windows
07:22
Speaker A
Okay, and by using that window ID, we will do certain types of operations like switching between multiple browser windows or if you want to perform actions on multiple browser windows. If you want to achieve this, we need this window ID. So that window ID we cannot find anywhere, even in the DOM also. It will not be able to generate even in the DOM also. We cannot see that. Only at the time of browsing the web page or at the time of opening the web page, that ID will be created by the browser itself at runtime, and that ID will not be the same. If you open google.com page now, the ID will be different. Next time it will be different. Every time when you open the browser or when you refresh the browser or when you open the application, every time the ID dynamically will change.
07:43
Speaker A
handles returns IDs of multiple browser window so I will discuss them in detail with an example later so these are the basic get commands or we can say get method okay so that ID contains some combination of numbers and characters it is ID is basically in the form of string
08:04
Speaker A
So if you want to capture that ID, we use this command getWindowHandle. And sometimes, if you are opening multiple browser windows, let us say, okay, I'll try to open some application. Let's say I have opened one application here. So this window is having one ID by default, and when you're trying to navigate to some other window, suppose I'm clicking some link and it is opening another tab or another browser window. Now currently, I have two browser windows.
08:21
Speaker A
get current URL get P Source get window handle get window handles all right so now we'll see practical examples and how and where we can use these commands let's go to eclipse and uh let's create it is just give only ID and we can also call it as a handle the value of the
08:51
Speaker A
Now I want to capture the IDs of all the browser windows currently open. So currently, two browser windows are open. So I want to get these two IDs dynamically. In that case, we can use this getWindowHandles.
09:13
Speaker A
methods okay so now let us try to use this commands uh by taking some application so I'm taking this orange HRM application so I'm trying to launch a new web page I can say web driver we have to first create web
09:36
Speaker A
Okay, so getWindowHandle returns the ID of a single browser window, whereas getWindowHandles returns IDs of multiple browser windows. So I will discuss them in detail with an example later. So these are the basic get commands, or we can say get methods.
10:07
Speaker A
is the URL of application just copy this URL and paste it over there now so as soon as you launch my web page okay so the first command is what get so what is a get command is doing get is a
10:25
Speaker A
Okay, so that ID contains some combination of numbers and characters. The ID is basically in the form of a string. It will return.
10:44
Speaker A
we are launching the URL or we are opening this application URL on the same browser and will it return return anything for us get method so get method doesn't return return anything it is just saying void get method will not return return anything but it will take parameter and
11:01
Speaker A
Okay, getWindowHandle returns IDs in the form of a string. So we will discuss that later in detail. So this is not the right time. So I'm just listing out all the methods which come under get methods: get, getTitle, getCurrentURL, getPageSource, getWindowHandle, getWindowHandles.
11:27
Speaker A
see the title if you inspect this a particular web page on the header tag you can see one new tag called title here so this is the title orange HRM this is a title of the web page okay and this title will uh will be different from one page to another page so if you want
11:48
Speaker A
All right, so now we'll see practical examples and how and where we can use these commands. Let's go to Eclipse, and let's create... It is just given only ID, and we can also call it as a handle, the value of the handle for every browser window, which every browser window is having, that ID will be generated at runtime.
12:10
Speaker A
title so what is the written type of this this written type of the method is string so get title method will return return the title in the form of string so you can store that value in a variable then you can use it for comparison or if you want to directly print you
12:29
Speaker A
Okay, so now let's create a new package and say Day 26. Inside this, I'm creating a new class. I call it getMethods.
13:07
Speaker A
okay now you can see this is the title of the web page right so similarly we have another command called get current URL so this will return the URL of the current web page so that is exactly equal to this one so whatever URL we open so
13:29
Speaker A
Okay, so now let us try to use these commands by taking some application. So I'm taking this OrangeHRM application. So I'm trying to launch a new web page. I can say WebDriver. We have to first create WebDriver. Can you see my screen, guys? WebDriver driver equal to new ChromeDriver, right? So Control Shift O.
13:59
Speaker A
and if you still want to store in a variable you can store in the variable and then you can compare with your expected URL as part of validation this is get current URL yes so we still not done anything on the web page just we are capturing the required
14:23
Speaker A
information so now you can see this is a URL of the web page so it is exactly same as this one as in the beginning whatever URL we open same URL it will be written get current URL so next one
14:40
Speaker A
get page source and this will return page Source but what is the use of this paid source so what is the necessity of this normally PID Source means if I right click and view pay Source it will show you the pay Source this is actually written by the server but we cannot understood
15:00
Speaker A
this particular language right so the browser will convert this into UI format so in the UI format we can see everything but if you page is in in this particular format we cannot understood right so if you want to capture the source page pay Source we can use this command but what is
15:17
Speaker A
the use of this page Source sometimes if you do if you want to do some validation on this page Source let's say uh some text is present on the page Source or some element is present in this page source or not and if you want to check first we need to capture the pay Source
15:34
Speaker A
okay and then we will check or we'll do some validation on the pay Source okay but this is not that much important we don't do uh source code validation most of the times but they have given some command like this this is pay Source let's try to execute this driver. get page source
15:59
Speaker A
and this will return the entire page source so in the string format so that try to print it driver. getp Source okay now you can see this the console window we got entire the pay source so this this is all pay source which is written from the application so from here it is started from HTML
16:35
Speaker A
is the root node right so everything it is so which contains HTML JavaScript combination of code because normally web pages are designed by using HTML and then combination with the JavaScript so this is get page source and we never use this command most of the time is not
16:54
Speaker A
much important and the next is get window handle get window handle so these are the most important two commands most of the times we use this in our automation so for now just understand what exactly this will return but once it is return window ID with that window ID we will perform some further
17:16
Speaker A
operations okay I'll show you that also first we'll see how we can use this get window handle so this will return the ID of the single browser window single browser window Okay so so let me just a second
17:36
Speaker A
yeah okay so just commenting this Bas source for now right so now we'll see get window handle so we already launched this particular page okay so now I want to capture the ID of this current web page can say driver. get window handle so this will return the string type of data so I can just print
18:04
Speaker A
it okay let me store this so basically get window handle will return the string type of output so the ID will be present in the form of string so I'm taking one string variable string window ID and after that I'm trying to print window ID okay so I can also put some small text window ID okay
18:35
Speaker A
now as I said this window ID is not a constant value so this will keeps changing dynamically at the run time because this ID will be created by the browser at the run time so if you look at here
18:49
Speaker A
I'm launching my browser opening the URL so this is the window ID which is got returned see this this is the window ID okay now next time again I will run one more time now this time you can see
19:03
Speaker A
it will return another type of window ID because this will not be the same every time yes now this time it is written on another type of window ID okay this is in string format see now you can't
19:21
Speaker A
compare these two because both are totally different type of IDs which are generated so whenever you are launching a new browser window as soon as you launch a new browser window the ID keeps changing this is dynamic okay so this is a get window handle get window handle will return
19:40
Speaker A
the ID of the browser window single browser window now sometimes if I open multiple browser windows then I can capture the IDS of multiple browser windows at the same time I'll tell you what is the use of this command first lesson how we can use it okay get window handles it will also Returns the
20:04
Speaker A
IDS of multiple browser window so whenever you want to use this command uh if you have at one if you have one single browser window anyway it will return single ID but if you have a multiple browser windows are open then it will return IDs of all browser windows okay so for example uh
20:22
Speaker A
what I will do is uh this is the web page we are currently opening right so I will comment this window ID also right so this is a web page currently opening so this is having one ID so
20:36
Speaker A
after opening this window ID so what I will do is I will try to click on this particular Link in the bottom orange HRM ink so once you click on this link it will open another browser window so now
20:50
Speaker A
I have a two browser windows and once you open the second browser window then I will use get window handles commands so that we will know exactly whether it is returning IDs of both the browser windows or not okay so what I will do is this page we already launched at the beginning now I will
21:09
Speaker A
perform click action on this element R HRM Inc so that it will open another browser window so let's inspect this element and uh yeah you can take this as a partial link text or you can use as an xot
21:24
Speaker A
anything is fine right so I'm taking this link text and try to click on this element so driver dot find element by dot link text and this is a link text Dot click so this should be do click okay so once you click on this element then what happens it
21:54
Speaker A
will open another browser window so this action or this will open new browser window so that is the reason we perform click action okay so once it is open new browser window now I'm going to use get window handles driver dot get window handles yeah so now what is the return type of get window
22:24
Speaker A
handles so get window handle will return single browser browser window ID so it is a string get window handles will return multiple browser windows in the form of strings every window ID is a string but this time it is rning multiple right not only single multiple window IDs so the
22:46
Speaker A
return return type of this method is set set collection will be return returned and what is the set contains all are strings because all IDs we are going to store in the set collection so here I'm taking one variable called window IDs now the type of variable is set of string
23:10
Speaker A
set of string now this set collection we can import from java. so these are the collections already used in selenium okay so the get window handles return set of string why it is a string here because all IDs are in string format okay why it is set why can't we can use a list collection
23:33
Speaker A
here can anyone tell me the reason why they have used set instead of list collection yes so window IDs are unique so all window IDs each wind each browser window is having different type of ID okay they will not be the same so set collection doesn't allow the duplicates right but
23:59
Speaker A
if it is a list collection it is allowing the duplicates but there is no possibility to have a duplicate IDs so that is the reason they have created set collection the window handles return set collection okay now if you want to print So currently we open two browser windows so
24:19
Speaker A
it is returning IDs of two browser windows now this variable contains the two browser windows now I want to print them how we can how we can get those browser windows from the set collection from the set collection how we can extract window IDs okay
24:42
Speaker A
let's try to print it directly I just put this variable inside this Sprint just observe yeah it is giving an error so let's see what is an error it is giving so whenever you get an error or exception just try to read it so that
25:07
Speaker A
you will know some information yes no such element exception comes and when you will get the No Such element exception in which scenario you will get the No Such element exception whenever it is unable to locate element then you will get an exception and
25:25
Speaker A
why it is unable to get unable to locate element what element it is this is the element orang HRM link element it is unable to locate it is not saying element not found element it is saying no such element exceptions so whenever you say no such element exception most of the times it
25:44
Speaker A
is because of synchronization problem because the page is loading slowly but by the time it is trying to find this element but if this element is not still available on the web page then it is throwing unable to locate an element okay then what is the solution for
26:02
Speaker A
this for now just add one weight method you can add weight method Anywhere But as soon as you launch my web page here I'm adding This Thread do sleep and I can put 5,000 milliseconds so this will also we will discuss as part of uh weight commands later all right so
26:26
Speaker A
now if you execute this you can see the actual operation so first it should launch my browser then it should click on the link right so after 5 Seconds yeah it is clicked on the link then it is
26:42
Speaker A
open another browser window now I have a two browser windows first one and second one now if you look at here it is written the entire set collection so the first value is this one the ID of the first browser window this is the ID of the second browser window but it is wring in the form
27:02
Speaker A
of set but if you still want to get individual values how we can get individual values either you can convert this into ARR list and use get method or you can use a looping statement you can use for each Loop and then you can get individual values okay that's the process okay that we'll see
27:23
Speaker A
later so this is about this is how we can capture the IDS of multiple browser Windows okay so for now just remember only this much and what we can do with the window IDs what is the use of this
27:37
Speaker A
window IDs so sometimes what happens is if you have only one single browser window like this if you want to close this automatically then how we can close through automation I want to close this my browser window you can simply use driver. close command or you can use driver. git command
27:59
Speaker A
so they are all comes under browser methods actually there are two commands close and quitter suppose when I'm using close command and when I'm using quit command what is a major difference here is close command will able to close only one single browser window at a time suppose I have
28:17
Speaker A
one browser window like this I can close this directly because I have only one browser window and suppose you have multiple browser windows suppose I have a two browser windows windows or you may have n number of browser windows now I want to close all of them at one shot or I want
28:34
Speaker A
to close all of them at a time then you can go with the quit command so quit command will close all the browser windows at a time whereas close command will close only one browser window at a time okay that's a major difference but now my question is let us say I have multiple browser
28:52
Speaker A
windows like this multiple and how many are there now 1 2 3 4 5 in this five browser windows I want to close some of the browser windows based on my choice let's say first and second I want to close
29:08
Speaker A
or second and five I want to close or three and one one and three I want to close so based upon the choice I want to close that particular browser window so how it is possible this is very popular
29:21
Speaker A
interview question remember this if you if you are using close command and only one browser window window will be closed whichever browser window is got focused by the driver that will close when I use quit command it will close all the browser windows at a time now based on my choice I want to
29:40
Speaker A
close specific browser window then what I should do is first you need to capture the IDS of every browser window and then you have to Loop it each and every browser window you have to capture based on the browser window ID we can get the title and then we will compare the title with your expected
29:58
Speaker A
title if both are matching then we will close the particular browser window so that's the process but before that before using this window IDs we should know how to switch between multiple browser windows so currently uh normally what happens is I open this web page this is my first web page
30:17
Speaker A
I open now where is my current driver is focused on this particular browser window as soon as you click on this link it is open another browser window now where is our driver focused first page or second page what is your guess so as soon as open as soon as I click on this link it open
30:37
Speaker A
another browser window now I have two browser windows where is our driver focused so most of the times we think driver is will focus on the second web page but no driver is still focused on the first page it cannot directly switch to one page to another page until unless we do it okay
30:58
Speaker A
so as soon as you click on this link even though it is still even though it is opening another web page like this but still driver is focused on first browser window only okay but now I want to
31:13
Speaker A
perform some action on the second browser window so here I can do some actions username password everything I can do but as soon as you open the second browser window and here I want to perform some action let's say I want to click on some link here I want to verify something in the second page
31:29
Speaker A
but driver is still focused on the first page so how we can get the driver switch to One browser window to Second browser window so this is possible using switching commands Okay so in the coming sessions I will introduce those commands how we can switch between One browser to another
31:50
Speaker A
browser how we can switch our driver to One browser to another browser so once it is Switched how we can switch to switch One browser window or another browser window what is the source what is the parameter we requir is window ID so if you know the window ID by using that window ID we
32:11
Speaker A
can switch to One browser window to another browser window okay suppose if you know this window ID this browser window ID I can easily switch from first browsers to Second browser window then I can perform the action so where exactly in that particular scenario we will
32:28
Speaker A
use these two methods get window handle get window handles but I will show you that scenario later just I'm giving you some basic uh understanding so where exactly we use this get window handle get window handles but before learning these two methods we have to know how to switch between
32:48
Speaker A
multiple browser windows we have a separate kind of switching commands are there so once you understand the switching commands then again we will come back and discuss about get window hand get window handles probably in the next session I will show you that example okay so these are the
33:04
Speaker A
two methods are available for now just remember get window handle will return the ID of single browser window get window handles will return the IDS of multiple browser windows okay so that is enough so get window handle will return string get window handles returns string of set
33:27
Speaker A
collection string of set collection even this is also keeps changing same ID you will not get every time okay so these are all comes under get methods all methods are accessible from the web driver instance understood everyone so these are the get meths so far everyone is clear please confirm in
33:51
Speaker A
the chat box now I'll move to the next one very simple very easy I can take any web page and you can explore these methods you can apply these methods on any type of web page randomly you can
34:04
Speaker A
take some web page from the internet you can try these commands just do one round of practice okay so now let us move on to the next one conditional methods they are very very important conditional methods okay so conditional methods we can access from the web element not from the driver okay
34:31
Speaker A
conditional methods we can apply on the particular web elements conditional methods we can apply on particular web elements and get methods will not work on the web web elements these get methods will work on the web pages okay so get get title current URL Source handle these are all related
34:52
Speaker A
to web page but not web element remember the difference okay all get methods will work on the web page level not at the element level we are not going to interact with any element here we are just used this methods only by launching the browser window that's it okay whereas conditional
35:12
Speaker A
methods we can access or we can apply these methods on the web elements okay we need some web element on that web element we can use this conditional method so there are three methods are there and these three methods will return true or false Boolean value will be return is displayed is
35:33
Speaker A
enabled is selected these are the three methods are there is displayed is enabled is selected so get text is another method which we can also use along with the along with the web element suppose if there is an element if you want to capture the text of that web element you can use get text so
35:55
Speaker A
get text method will not comes under under this category why because these get methods will access only from the directly from the page but get text method will related to web element okay get text method we can access only from the web element not from the web page not from the driver but these
36:17
Speaker A
get methods we can access only from the driver instance whereas get text method we can access from the web element I will show you that also but that is not comes under this particular cat right so get uh here conditional methods so conditional methods we can use on top of web element will
36:38
Speaker A
perform EAS displayed means what it will check that element is displayed or not it can be any type of element it can be image or input box link button anything that is displayed on the page or not that we can verify by using e displayed and is enabled Sometimes some elements will be disabled
36:58
Speaker A
an enable State and we can check that element is still enabled or not and that element is in working condition or not we can check by using ease enabled method and this is also we can use for any type of web element ease selected and this method especially we can use for uh radio buttons
37:18
Speaker A
and checkboxes means what suppose the radio button is there you already selected and when I use this method it will check the radio button is already selected or not if it is already selected returns true if it is not selected return false same thing for checkboxes also I have multiple
37:34
Speaker A
checkboxes I selected some of the checkboxes but some of the check boxes are unchecked so this command will check whether the checkbox is selected or not if it selected returns true if it is not selected return false okay so these are the conditional methods we can access from the
37:51
Speaker A
web element not from the driver okay remember and these conditional commands will return a Boolean value which is true or false all right so now we'll see the practically how we can use this commands let's create another example new
38:16
Speaker A
L additional methods okay so this time I'm trying to open some other web page so web driver driver equal to new Chrome driver and then I'm launching my packages and this is the page I'm launching this time so I can just look at this n Ecommerce ation
38:58
Speaker A
page okay now we can see some small registration form is there and there different type of web elements some radio buttons are there input boxes drop downs and some checkbox also here right some elements are there okay so we'll try to use this conditional commands on this particular web page
39:21
Speaker A
also uh as soon as you launch my page like this small window I want to maximize it so how can we maximize the page driver dot manage dot window do maximize so this command will maximize the web page okay now the first command is what is displayed is displayed so this particular
39:49
Speaker A
command we check the element is displayed or not okay for example let's say I want to verify this logo is displayed or not this logo is present right I want to verify this logo is displayed or not so what I can do first we will capture this as a web element now inspect this element
40:10
Speaker A
and uh you can create your own xath or you can directly go to selector hub from the selector Hub you can get the expath of the image okay this is a IMG so this xath so I'm getting this xath and
40:25
Speaker A
capture that element driver Dot find element by dot X paath okay and this is the web element now I'm storing into a variable now tell me what is the type of this variable type of this variable is what type of the variable why it is Boolean did I use display method on this web element
40:54
Speaker A
did I use any EAS displayed method no right but why you are saying Boolean so this will return always web element find element method will always return a web element so this logo is what is a web element variable okay it is a web element variable we have to import this right so
41:15
Speaker A
once you get this web element then we can apply the conditional methods okay now I want to check this logo present or not simply what you can say logo dot is displayed is displayed and this will return a Boolean value true or false that I can print system do do do print Ln logo dot is
41:41
Speaker A
displayed okay so here I will put something I can say display status of logo like this so before before using this conditional method we first we need to capture the web element so we captured this entire web element in a
42:06
Speaker A
variable and then by using that variable I call this e displayed method so from where we are accessing this method from the web element class not from the driver okay now execute okay now open maximized now can go back and see yeah display status of the logo is true
42:32
Speaker A
it is displayed now I will write something you guys can tell me whether it is correct or wrong I'll write driver. find element by. XPath and also I'm using same thing same X paath okay dot is displayed and here I'm taking a boan variable called status and now here I'm printing display
43:08
Speaker A
status status okay now tell me is this statement correct or wrong correct or wrong can we write like this yes why because we are identifying the the element here once it is identified instead of storing that element in another variable I'm directly
43:33
Speaker A
using is displayed okay now final method is what here is displayed so what this will return a Boolean value so this variable should be Boolean and that I'm going to print but in the first case what you have done first we capture the element in a variable and then I call this
43:50
Speaker A
e displayed with the variable and then I printed directly and you can also write like this anything is correct I hope this is clear to everyone it's very basic on context okay now easy displayed method you can apply for any type of web element so you can also check this input box is present
44:14
Speaker A
or not this label register is present or not okay and anything you can check on this web page and the E display method is applicable for all kinds of elements now the next method is enabled is enabled okay and this method especially used for input boxes most of the times we'll use
44:40
Speaker A
it for input boxes and uh drop downs radio buttons and checkboxes okay because images we don't do any actions right so even image is enabled or not nothing to do with that right but suppose input box is enabled then only we can pass some text okay suppose here I
45:04
Speaker A
want to search for something if you want to type something that input box should be enabled State okay and if it is disabled State I cannot pass any value into it that's called disable status enable and disable enable means it is allowing us to provide some data disable means it is not
45:22
Speaker A
allowing to provide some data sometimes you can see disabled fields in our application so in those cases you cannot part you can you cannot pass any value into it that's called disabled status so most of the times before passing the value in it we will can check or we can check this
45:38
Speaker A
input box is enabled or not that's a conditional statement okay so let me check some element is enabled or not in this particular web page so let me check this one search store is enabl or not or you can check the first name is enable or not let me inspect this element first name I'm taking and
46:05
Speaker A
uh can go back to the xath select our Hub okay this is the xath which is given now I'm using this XPath and this also is enabled also will be used on top of web element so I can say driver.
46:21
Speaker A
find element by. X paath okay now if you want to store this in another element you can store another variable or else you can directly use that method dot is enabled and what this will return what is the type of this variable what is the type of this variable it should be volan okay
46:52
Speaker A
so once you capture the web element on top of web element we apply this method e is enabled so this will check enabl or not if it is enabled returns true if it is not enabled then returns false and before passing the value in the input box we can cross check whether that element is
47:09
Speaker A
enabled state or not if it is enabled State then we can continue if it is not enabled then would don't contate so in as a part of validation you can use this method especially the conditional methods we use as part of validations so here I'm just printing enable status
47:31
Speaker A
okay that's it and suppose if you apply these methods for any disabled Fields then it will return return false yes I already told you right so this method you can use it for any type of elements any type
47:51
Speaker A
of element but there is no use radio buttons checkboxes that is fine but what is the use of uh using that method on the logo some static test no use right so this will return true enable status is true okay so when it is a valid you can use it for anything but
48:11
Speaker A
the problem is suppose if we are using enable or disable can we check that logo is enable or not do it make any sense nothing right so for example label enable or not is it make any sense nothing right but where we can use exactly is suppose if you want to perform some
48:34
Speaker A
action on that element suppose radio buttons so before performing an action you can check whether it is enabled or not so that is a valid it makes sense same thing for input boxes also we can use it because we can do some kind of operation here enable or not you can check same thing drop downs
48:50
Speaker A
you can check check boxes also you can check okay but it doesn't make any sense if I use for labels or images nothing to do with this enable or not even buttons also you can check whether button is
49:05
Speaker A
clickable or not enabled or not if it is enabled then only we can click if it is enabl if it is not enabled we cannot click okay other than the static kind of elements all other type of elements
49:17
Speaker A
we can apply static elements in the sense there is no functionality associated with it is just display on the web page but there is no action for those elements they are called Static elements okay apart from those type of elements you can apply these methods for any type of web elements
49:35
Speaker A
yes that is also a true scenario sometimes submit button is not enabled so until unless you provided all the fields so then what you need to do first you need to provide the fields provide some data and after that you can check the submit button is enabl not so before providing the data should
49:52
Speaker A
be disabled after providing the data should be enabled both the scenarios we need to validate as part of automation okay so that is also part of the validation so before providing the data it should be in disabled State that's is our expected after providing the data it should be enabled that
50:10
Speaker A
is also our expected okay suppose as part of your requirement as part of your requirement after providing the data also the submit button is not enabling then it is a bug okay after providing data so the submit button is not enabled that is a bug so before providing it should be it
50:32
Speaker A
should be disabled after providing it should be enabled that's our expected so we need to also do the negative testing like that okay so these are the enabled method so EAS displayed method we'll check the element is displayed or not enable method will check the element is enabl
50:53
Speaker A
or not in other words we can say the element is operational or not okay element is displayed or not element is operational or not can say can we can check display status of the element is enabled we can check enable or disable status of the element especially this is operational element
51:28
Speaker A
not static element okay now can we first use get for particular page followed conditional method so you can use anywhere okay you can use conditional method first then you can use get methods next voice Versa that is up to your test case based on the test case you have to follow so
51:50
Speaker A
in the test case you have a steps right 1 2 3 4 the same order we have to automate the test case we need to consider the steps of the test case case not the commands Okay according to
51:59
Speaker A
the steps whichever command is applicable there or appropriate we will use that command so you need to follow the test case while automating the test case okay now the next method is is selected and this method we can use we can use to check the element element is selected
52:28
Speaker A
or not selection so selection means what it is applicable for checkboxes because checkboxes we can select uncheck or we can select or unselect radio buttons also we can select or un select what else we can do yeah radio buttons and checkboxes mostly we will use is
52:50
Speaker A
selected method and sometimes drop down also this drop down we can select some value as soon as you selected some value from the drop down we can also use e selected method whether the drop down is already selected value or not we can check by using e selected method so we this but method we
53:09
Speaker A
can apply for check boxes radio buttons and also drop downs is selected yes now let us try to use it so the next method is select okay so on the same web page so I have a two radio buttons are here so by default none of them are selected male
53:42
Speaker A
reveal by default is selected or not no one is selected okay now if you apply this method EAS selected method and male and female radio buttons what is an output you will get because none of them are selected by default no nothing is selected so when I apply e selected method
54:01
Speaker A
on male and female on both by default I will get false because they have not selected okay so before applying what I should do is we will capture these two elements radio buttons we have to capture male and female both I will capture so how to capture it inspect this element you
54:20
Speaker A
should inspect the the round shape not on the label okay so you need to inspect like this this is the right way to inspect not this one or not this one also you need to exactly point the round symbol now there is an input tag is there even for radio button check boxes input
54:39
Speaker A
boxes for all of them you can see same tag name called input so this is input tag okay now I can use a locator so you can see there is a xath is got generated so I'm using this
54:51
Speaker A
xath you can use any type of Loc a CSS or whatever you want you can use this so I'm capturing this element I can say driver dot find element by. xath and I'm storing this element in a variable this is a male element I can say malecore Rd male radio button and what is the
55:17
Speaker A
type of this variable what's the type obviously it is web element everything is a web element okay same way I will also capture female radio button let's inspect that element yeah this is a another radio button driver dot find element by. X paath and I'm
55:49
Speaker A
storing this also because I want to perform some further actions on these elements that's the reason I'm storing into the variable and if you don't have any further options directly you can here you can use e selected method okay web element and this is female _ Rd okay so
56:10
Speaker A
now I have captured both elements now just observe I'm using malecore Rd element dot is selected and I'm printing something here so before selection so before selection I'm trying to find the selected status of male AO button and also
56:39
Speaker A
female AO button so what is our expected false because none of them are selected at the beginning both are not selected so obviously we are expecting false okay let's see whether it's is rning false or not before selecting any of the radio buttons
57:03
Speaker A
okay so I have not selected anything just by default is empty now if you go back and see before selection both are false before selection both are false okay now I want to select one of the radio button okay so here I'm printing after
57:27
Speaker A
selecting mail radio button after selecting mail again I will print both okay but how to select if you want to select any radio button simply what you can do take that element dot click just use click method so this will select radio button okay so as of now which
57:54
Speaker A
one I'm selecting male radio button selecting so this will select male radio button okay so once you selected then again I'm printing the status of male AO button and female a button now tell me what is you expected here so male dat button have selected so what is the status
58:19
Speaker A
of here this is true yes what the status of the second obviously false because still it is not selected yes now we can see the radio button is got selected the first one is got selected so as per our expectation first one is return true because it is already selected second one is
58:48
Speaker A
still not selected it is still false okay now what I'll do is this time I will select female radio button after selecting female radio button so instead of clicking on male radio button I'm trying to clicking on female radio button so this will select female radio button okay so
59:11
Speaker A
now the result will be opposite right so this will return false and the second one returns true because in the radio buttons you can select only one at a time both you cannot select but if it is a checkboxes you can select multiple checkboxes instead of selecting one you can
59:38
Speaker A
select multiple one or multiple you can select so it has got selected female leer button here yes now you can see yes so after first initially both are false after selecting M button the male is written true female is return false after selecting female then it is returning male is
60:02
Speaker A
returning false and female is returning true so this is our expected values okay yes you can use it no problem because without using without selecting the radio button if you apply e selected method it will just return false because it is not selected that's the purpose of using e
60:22
Speaker A
selected whether that element is selected or not how we will check if you already selected then it will return true anyway if it is not selected it return false so you can use e selected method before selection or after selection at any time no problem okay you can still use it and before
60:40
Speaker A
selection if you apply e selected method it will return false and without selecting if you apply e selected method which will return false okay that is a method so is it clear everyone same thing you can try for checkboxes also so for checkboxes also we can check whether it is selected or not
61:03
Speaker A
so for example let me show you quickly this is the web page on the same web page I have something called newsletter right so if you open the page freshly and by default this new selector is got selected by default so can we apply e selected on this yes when you apply e selected
61:21
Speaker A
what is the value it will return true because which is already selected okay let's inspect this okay this is the newsletter element so now I'm applying on the newsletter enabl or not so just observe can say driver do
61:45
Speaker A
find element by. X paath and I'm I just want to verify is selected or not so I can simply directly I'm using is selected I'm not storing into another web element because I want to perform only one action and that I can store in a Boolean variable Boolean okay this is
62:07
Speaker A
what this a newsletter right so newsletter status right now what is the status you are expecting here is true so newsletter checkbox status I'm directly printing this all right so this will return true and for checkbox also if you want to check or uncheck we use click method so now new SL checkbox
62:52
Speaker A
is true okay so now is it clear everyone so is displayed is enabled is selected so these are the three methods which are comes under conditional method which are very very important in automation especially when you do validations okay first we will learn individual commands but once you start
63:15
Speaker A
creating the test cases you will do validations we will learn validations also okay so is displayed e enabled e selected methods widely used in validations whether the elements are displayed or not element is enabled or not elements are selected or not and E displayed method you can
63:35
Speaker A
apply for any type of element it can be static element or it can be operational element any type of element you can apply is displayed e enabled method we can apply on operational methods but it will also work in static uh elements but there is no use okay so most of the times it
63:53
Speaker A
will apply on the elements which are operatable or operational elements and easy selected method we can apply for radio button checkboxes and drop downs also okay if there is any drop down if the value is not selected from the dropdown then it will return true if it is selected value from the
64:12
Speaker A
drop down it will return false that is called E selected okay I will also discuss about drop downs in the coming sessions for now we can skip it so these are the conditional methods three methods now the next one browser methods so far every Everything Is Clear please confirm in the chat box
64:33
Speaker A
one second everyone conditional methods are clear yes now let us move on to the next one browser commands or browser methods only two methods are there we already discussed close and quit close and quit major difference there only one difference close
65:01
Speaker A
command will close able to close only single browser close command will able to close only one single browser whereas quit command will able to close multiple browsers multiple browsers at a time it can close so let me show you an example
65:28
Speaker A
okay so let's create I'm taking this get methods example okay so I'm trying to remove all these things okay just observe I'm just trying to launch this particular page also I put some weight statement okay now just observe here I'm clicking on some link I'll show you this this web
66:10
Speaker A
page okay yeah so first of all I'll open this web page and once you open it then I will click on this link so this will open another browser So currently how many browsers are there two browser windows are there so same action I have done here first I open this page then I click
66:35
Speaker A
on this element then I will have a two browser windows now let me use driver do close driver.
66:43
Speaker A
close I'm using okay just observe what will happen I'll put this weight statement here so that you will exactly notice which one it is got closing so two browser windows are open open I'm using close command okay this is my first browser window yes there is a problem with the synchronization so
67:09
Speaker A
what I will do I'll put another weight statement after launching my browser okay now you can see two browser windows are open first one second one now just observe the first one is got closed the first browser window which is called as a parent window is
67:42
Speaker A
got closed the second browser window still alive what does it mean so I already told you one point whenever you open two browser windows the driver will focus on the first page it cannot shift to one page to another page immediately so even though it
68:01
Speaker A
is open another browser window second browser window still our driver is focused on first browser window so that is the reason the command close command is closed only first browser window okay so now I want to close both the browser windows then I can use quit command
68:22
Speaker A
I can say driver Dot quit this will close both the browser windows one and two let us see okay this is my first browser window just observe after five second browser window is also opened now after 5 Seconds both will be closed yes both the browser windows are closed
68:54
Speaker A
so that is a major difference between close and quit so close command can close single browser window and which browser window again in two browser windows are open only single browser window will be closed which one is that wherever the driver is focused okay close single browser
69:19
Speaker A
window wherever the driver is focused okay and quit command will close all the browser windows right so this is a major difference between close and quit but again based upon my choice I want to close parent or I want to close child depends on my requirement how we can control
69:50
Speaker A
we need to know the switching commands how to switch between one page to another page after switching you can close it okay that we'll see later so remember that point interview prospective which is most important so get window handle get window hand along with the switching commands we
70:06
Speaker A
will use it okay these are all browser commands so three kinds of commands or methods we learned so far get commands or get methods conditional methods and browser methods okay so we also have a navigational methods and then weight methods in the tomorrow session we will discuss this okay
70:36
Speaker A
navigational and weight methods weight methods are most important we have implicit weight explicit weight fluent weight and thread do sleep also we'll discuss something advantages disadvantages you will get more in questions on this so tomorrow session we will cover navigational commands and
70:55
Speaker A
then wait okay so as part of assignment what you guys can do is I will show you one thing assignment so can just go to this web page I'll show you test automation practice blocks.com so go to this page and in this particular page whatever commands today we learn apply all the
71:24
Speaker A
commands so just find out the title of the web page capture this URL capture the page Source okay and what else we learn get the window handles okay and get the window handles IDs and after that try to interact with these elements check boxes check boxes are there and also radio buttons are
71:48
Speaker A
there you can check enable and disable status of radio buttons and checkboxes and also we can check enable and disable status of the the input boxes okay so you can try this for today's session and tomorrow I'll give you some more assignments so do some kind of a practice on this so whatever
72:08
Speaker A
topic so far we discussed just apply these topic on this page so that is a assignment for today and tomorrow we will continue with the rest of the commands navigational and then weight commands Okay so I'll stop here for today's session tomorrow we will continue
Topics:SeleniumJavaWebDriverWeb elementsGet methodsAutomationBrowser automationWindow handlesXPathSDET

Frequently Asked Questions

What is a web element in Selenium?

A web element in Selenium is any element present on a web page such as input boxes, checkboxes, radio buttons, or dropdowns that can be interacted with during automation.

What are get methods in Selenium WebDriver?

Get methods are predefined WebDriver commands starting with 'get' that retrieve information like the page title, current URL, page source, and window handles.

How does Selenium handle multiple browser windows?

Selenium uses getWindowHandle and getWindowHandles methods to retrieve unique, dynamically generated IDs for browser windows to switch and perform actions across multiple windows.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →