Session 28 – Selenium with Java | WebDriver Methods | B… — Transcript

Learn Selenium WebDriver navigational commands in Java, including navigate.to, back, forward, and refresh with practical examples and differences from get method.

Key Takeaways

  • Selenium WebDriver provides four main navigational commands accessed via driver.navigate().
  • driver.navigate.to() and driver.get() both open URLs but differ in implementation and accepted URL formats.
  • navigate.to() can accept both string and URL object formats, while get() accepts only string URLs.
  • Navigational commands like back, forward, and refresh help automate browser navigation effectively.
  • Understanding these commands is essential for robust Selenium test automation and common interview questions.

Summary

  • Introduction to Selenium WebDriver navigational commands in Java.
  • Explanation of four main navigational methods: navigate.to, navigate.back, navigate.forward, and navigate.refresh.
  • Demonstration of how to use the navigate method to access these commands via the WebDriver instance.
  • Comparison between driver.get() and driver.navigate.to() methods for opening URLs.
  • Clarification that both get() and navigate.to() achieve the same functionality but have different internal implementations.
  • navigate.to() accepts URLs in both string format and URL object format, whereas get() accepts only string format.
  • Practical coding example using Eclipse to create WebDriver instances and use navigational commands.
  • Use cases for navigating between multiple pages within the same browser session using back and forward commands.
  • Explanation of the refresh command to reload the current page.
  • Discussion on common interview questions regarding differences between get() and navigate.to().

Full Transcript — Download SRT & Markdown

00:04
Speaker A
So in the previous class, we have started learning WebDriver commands or WebDriver methods. We have seen get methods, conditional commands or conditional methods, then browser commands, and then we have seen wait commands. The last one is navigational commands.
00:20
Speaker A
commands so there are four types of navigational commands are there and these commands are the methods we can access through navigate method so in the driver we have something called navigate this is a method which is available and from this method we can call another method called two and
00:47
Speaker A
So there are four types of navigational commands, and these commands are the methods we can access through the navigate method. So in the driver, we have something called navigate. This is a method which is available, and from this method, we can call another method called to.
01:13
Speaker A
navigate. forward driver. navigate. refresh so the first we need to call the navigate method and which will return a navigation object from that we are calling another method called two back forward and refresh okay let's see what is the use of these commands so sometimes uh if you want to
01:31
Speaker A
And there are a few more methods: navigate.to, navigate.back, navigate.forward, and navigate.refresh. So these are the four methods, and again this navigate method we can call from the driver. So overall, the command is driver.navigate.to, driver.navigate.back, driver.navigate.forward, driver.navigate.refresh.
01:53
Speaker A
on the same browser suppose I want to open some other application let's say I have open another application like this okay as soon as you open another application the previous application is gone but still if you want to go to that particular page we can use
02:11
Speaker A
So first, we need to call the navigate method, which will return a navigation object. From that, we are calling another method called to, back, forward, and refresh. Okay, let's see what is the use of these commands.
02:34
Speaker A
same thing we can do through commands navigate. to back navigate. to forward navigate. to refer navigate. refresh so the third option is a refreshing so this is basically reloading the page so refresh command we try to reload the page so these three operations we can do through
02:51
Speaker A
Sometimes, if you want to navigate between multiple pages, normally we use these methods. So let me show you some page here. Let's say I try to open this page. So as of now, this is the current page I have open, right? So on the same browser, this is the first page I open.
03:10
Speaker A
getet and URL that will open the application URL in the browser right same thing navigate.
03:15
Speaker A
On the same browser, suppose I want to open some other application. Let's say I have opened another application like this. Okay, as soon as you open another application, the previous application is gone. But still, if you want to go to that particular page, we can use these options, right?
03:30
Speaker A
28 so say navigational commands Okay so first time uh I'll try to launch some application can you can take any application I'm saying I'm taking some other application this is demo. commerce.com so normally how to launch our web page you can say driver. get and before that we need to create a
04:11
Speaker A
This is the arrow key; it will go back and it will show you the previous application. Okay, and again, if you want to get another page, you can say forward. So these two options we can use to move forward and backward. So normally, through pages, we can do like this.
04:36
Speaker A
driver dot navigate do2 and here we can put the URL so driver. naate do2 so the functional Dev Dev both are exactly the same no difference but internally the implementation is different get is a implementation is different navigate. to implementation is
04:57
Speaker A
The same thing we can do through commands: navigate.to, back, navigate.to, forward, navigate.refresh. So the third option is refreshing. So this is basically reloading the page. So refresh command we try to reload the page.
05:14
Speaker A
this yeah now we can see the same URL is got open on the browser okay so now let us try to understand and uh the difference between these two so most of the very popular in question also they ask what is the difference between get and navigate. 2 so functionality wise there
05:41
Speaker A
So these three operations we can do through automation through these methods: navigational methods to, back, forward, and refresh. Back, forward, refresh, and navigate.to is a method which will take URL as a parameter, and this will open the URL on the browser. It is exactly the same as a get command.
06:01
Speaker A
second yeah so let's try to understand this so normally when I use get method okay so we'll pass the URL in the string format right we are passing the URL in the string format and internally they have written some piece of Code by taking the string URL they have written some logic and
06:27
Speaker A
So we normally use driver.get and URL; that will open the application URL in the browser, right? Same thing navigate.to, and it will take URL as a parameter. This will also do the same. Okay, these are the four commands.
06:40
Speaker A
format whereas in navigate. 2 method here also we are passing string format right so the Navigator 2 method how it is got implemented is like this navigate. 2 method actually the two is actual method navigate is another method okay inside this two method also internally it is calling
06:59
Speaker A
So let me show you how we can use them. So let's go to Eclipse and create a new package. So say navigational commands. Okay, so first time, I'll try to launch some application. You can take any application. I'm saying I'm taking some other application. This is demo.commerce.com.
07:18
Speaker A
method this is also internally calling get method because a method can call another method right a method can call another method so when you call two method internally this is also calling same get method only so that means when I call get method or when you call navigate2 both are
07:35
Speaker A
So normally, how to launch our web page? You can say driver.get, and before that, we need to create a WebDriver instance, right? So WebDriver driver equal to new ChromeDriver. We need to import the package, and we can say driver.get, and this is the URL.
07:51
Speaker A
parameter whereas in a navigator two method we can pass this URL in the form of string and we can also pass the URL in the form of URL object in the form of URL object so what does it mean so let me show you so two formats are allowed navigate to Method string format is
08:14
Speaker A
So normally, this is the command which we normally use: driver.get URL. The same thing we can do by using another command called driver.navigate.to, and here we can put the URL. So driver.navigate.to.
08:41
Speaker A
only in the string format but here navigate. two method accepts the URL in the string format this is one thing the other thing is it is also accept the URL in the URL format means URL object format for example there is a predefined class URL for that I can create or object my URL equal to new
09:07
Speaker A
So the functional difference is both are exactly the same, no difference, but internally the implementation is different. Get's implementation is different; navigate.to implementation is different.
09:26
Speaker A
at this particular statement URL my URL equal to new URL so what I have done is I just created an object of URL class and in the Constructor I'm passing the URL now this URL is in the format
09:42
Speaker A
So I'll tell you what is the difference between these two commands: driver.get and the URL or driver.navigate.to or URL. So this will also do the same thing whatever get command is doing, this navigate.to command also doing the same thing.
10:11
Speaker A
use navigate. 2 method we can pass URL directly as a string or we can pass this URL as an object we can pass an URL as an object so both the formats are accepted in Navigator 2 method whereas in get method URL will be accepted only in the string format so this is a major difference
10:38
Speaker A
So let me run this. Yeah, now we can see the same URL is got open on the browser. Okay, so now let us try to understand the difference between these two. So most of the very popular interview questions also, they ask what is the difference between get and navigate.to.
11:00
Speaker A
only in the string format whereas navigate. 2 method accept URL in both the formats in string format and in url object format remember this so driver. getet method accepts URL in string format only whereas driver do navigate do2 accepts URL in the string format and URL object
11:38
Speaker A
So functionality-wise, there is no difference, okay? Whatever this get command is doing, so navigate.to method also doing the same thing, but there are a few differences between these two methods.
11:58
Speaker A
driver. getet me most of the times we pass URL in the string format only okay because we never convert that into again URL object and we never pass it that's not a use case most of the times we never do it but because they already provided some navigational commands related to the browser
12:15
Speaker A
Okay, so let's try to understand what are the differences between get and navigate.to method. Just a second. Yeah, so let's try to understand this. So normally, when I use get method, okay, so we'll pass the URL in the string format, right? We are passing the URL in the string format.
12:36
Speaker A
format let's say this object is captured from somewhere else now by using this object I want to launch my application so let's say you got an URL object from application or somewhere else in the form of URL object Now by using this object I want to launch my web page so then you can go
12:54
Speaker A
And internally, they have written some piece of code by taking the string URL. They have written some logic, and this will actually open this URL on the browser. So this is a get method implementation. It will take only URL in the string format. It allows only in string format. URL we can pass only in the string format.
13:14
Speaker A
you want to launch your application through URL you can use this one by passing URL in the string format most 99% of the times we use string format okay we never use any object but this is indirect way so if you get the URL object from different applic or some third part application then
13:32
Speaker A
Whereas in navigate.to method, here also we are passing string format, right? So the navigate.to method, how it is got implemented is like this: navigate.to method actually the to is the actual method; navigate is another method.
13:50
Speaker A
object if you want to launch your application this method will be useful but that is again very very very rare guess okay because I have never seen that kind of scenario anywhere most of the times we use only string URL okay so now we understood the difference between get get accepts URL only
14:10
Speaker A
Okay, inside this to method also internally it is calling get method. Me only internally it is calling get method. It will take URL as a parameter. And if you look at these two implementations, so when I call get method directly, we pass the string as a parameter, okay? That will launch the browser and open this URL.
14:24
Speaker A
create a URL type of object where we have to pass URL then this object we have to pass Ur okay this is a way okay I told you one more thing so navigate. 2 method also internally calling the
14:37
Speaker A
When you call navigate.to method, this is also internally calling get method because a method can call another method, right? A method can call another method. So when you call to method internally, this is also calling the same get method only.
15:00
Speaker A
right but get method accept only string but if navigate. to method is internally calling get method get method should accept object URL object right but it doesn't have accept but how it will work how it is validate so let us see how it is going to validate so what I said in
15:20
Speaker A
So that means when I call get method or when you call navigate.to, both are exactly the same because even navigate.to method also internally calls the same method, get method only. So functionality is exactly the same.
15:38
Speaker A
implementation will be called so if it is a string format no problem if you're passing a string even in navigate. to Method get method accept a string format so it will perform well but if you pass URL in object format okay if you pass this URL in the object format but internally how come the get meth
15:59
Speaker A
But what is the difference is in the get method, we can just pass only string parameter. That URL we have to pass only string parameter, whereas in a navigate.to method, we can pass this URL in the form of string, and we can also pass the URL in the form of URL object.
16:22
Speaker A
object format it will cross check because it will do some kind of a simple validation before calling get method so once you get URL whether it is string format or object format if it is a string format then directly it will call get method if it is an object format then it will convert the
16:40
Speaker A
In the form of URL object. So what does it mean? So let me show you. So two formats are allowed in navigate.to method: string format is allowed, URL format also allowed.
17:00
Speaker A
a string format it will directly pass it to the get method if it is an object format then it will convert into string format and then it will pass it to the get method ultimately get method only executed internally in the navigate. to Method okay so this is how we need to understand this so
17:18
Speaker A
So let me show you the difference. So we already know driver.get. Yeah, URL object I'll tell you that. So driver.get here we pass the URL only in the string format. Accepts URL only in the string format, only in string format. In get method, URL will be accepted only in the string format.
17:36
Speaker A
is this clear to everyone so very very important entry question and also lot of people cannot answer this because they're having some confusion on this and even if you search in Internet many people will say both are exactly the same and some people will say both are totally different
17:51
Speaker A
But here navigate.to method accepts the URL in the string format. This is one thing. The other thing is it also accepts the URL in the URL format, means URL object format.
18:08
Speaker A
us to store allow us to pass URL object that's the only difference otherwise both are same okay so now let us move on to the next one yes get method is a different implementation that is called from the Navigator 2 method in the last classes we already learned we can call one method let's say
18:29
Speaker A
For example, there is a predefined class URL. For that, I can create an object: myURL equal to new URL of here I can pass this URL, okay? And this is basically a URL object.
18:50
Speaker A
method so get is having another implementation get method is different so when I call driver.
18:57
Speaker A
So this URL we have to import from java.net package, and this will throw some exception. Just add this. Right now, if I look at this particular statement, URL myURL equal to new URL.
19:18
Speaker A
this let me comment this okay let's use this okay driver. navigate. to demo. n commerce.com so we never use object type of URL first thing okay because this is just additionally something is got provided but normally in automation we never use URL type of object but development
19:42
Speaker A
So what I have done is I just created an object of URL class, and in the constructor, I'm passing the URL. Now this URL is in the format of URL object, okay? Now the URL is not in string format. We're passing the string format again; it becomes an object, URL type of object.
19:56
Speaker A
itself not related to selenium it is not related to selenium it is a Java itself so normally the development team developers will use this kind of a classes and objects to work with the URLs but automation point of view we never do it we don't convert this into URL or we never
20:12
Speaker A
Now this object you can pass in the driver.navigate, driver.navigate.to, navigate.to, and here we can pass myURL. This is a way we can do it.
20:24
Speaker A
immediately what I will do is I say driver Dot navigate do2 I'll try to open another application let's say yeah I'll open this application so now what will happen when I run these two commands you can use get method also no problem so when I use these two commands
20:48
Speaker A
So when I use navigate.to method, we can pass URL directly as a string, or we can pass this URL as an object. We can pass a URL as an object. So both the formats are accepted in navigate.to method.
21:08
Speaker A
is open orange HRM now again if you go back to the previous application right if you want to go back to the previous application normally how we will do manually we click on this Arrow Mark right same thing we can do through automation suppose I want to go to the noop Commerce simply what you can do
21:24
Speaker A
Whereas in get method, URL will be accepted only in the string format. So this is a major difference between get and navigate.to, but functionality-wise, there is no difference.
21:43
Speaker A
command how to capture the URL of application yes driver dot get a current URL okay so after going back I'm getting the current URL so which URL expecting here the first one n Commerce URL you are expecting and after that again I want to move forward
22:09
Speaker A
So even if you execute this, it will do the same thing. I'm passing URL as a URL object.
22:38
Speaker A
s okay so now open orange HRM now you can see forward and backward it is navigated now we can see this is the URL which is returned by this command initially and this is the URL which is return by the next command okay so this is a way we can simply navigate forward and backward
23:03
Speaker A
using these commands but what is a prerequisite to use this command is first we need to open the URLs manually like this so by using get command or navigate. to Method any method you can use first we need to launch two applications and then you can go back and forward okay now if you want to
23:22
Speaker A
reload the application sometimes as soon as you're launching the page we do refreshment here right we can do refresh like this so if you want to refresh the page you have another command driver do navigate do refresh Navigator dot refresh so this command will refresh the page or reload the
23:48
Speaker A
page okay it is just to refresh the web page so very basic commands simple commands driver do2 driver navigate. back navigate. forward refresh so if you look at these commands first of all we are calling the navigate method so this navigate method return type is what navigation type
24:16
Speaker A
of object so from this navigation class we are calling the other methods refresh forward back to these methods are available in the navigation class okay this method will return the navigation object okay so remember this very basic commands just if you want to move forward backward you
24:38
Speaker A
don't need to provide URL multiple times okay so once you provided URLs then you can navigate back and forward every time you don't need to launch applications every time you don't need to forward and backward once you open these two applications by using back forward you can go
24:54
Speaker A
forward and backward so these are all navigational commands four command and driver. getet driver.
25:02
Speaker A
n.2 is alternative method so driver. getet method accept string format navigate. to Method accept string as well as URL object format okay so now so refresh is just reloading the page so normally if you want to refresh the page we can use this option right this is the button reload this page
25:25
Speaker A
you can see there are three buttons are there in the top corner back backward forward and reload for these three operations three methods are provided these are the three methods are provided for three different operations three methods are provided that's it equivalent methods
25:42
Speaker A
so instead of doing this manually like this right so we use those methods navigational methods all right so now let's move on to the next Topic in the last class uh we learned about brow uh we learn about get handle uh get window handle get window handles
26:08
Speaker A
do you guys remember those two methods get window handle get window handles get window handle get window handles right so we'll discuss more about this method actual use case where we use get window handle so we already know get window handle Returns the ID
26:33
Speaker A
of current browser window whereas get window handles Returns the IDS of multiple browser windows so can we use refresh command for single URL yeah whenever you want you can refresh the page yeah single URL also fine okay the refresh command you can also use with single browser no problem
27:00
Speaker A
so how often do you open multiple URLs on the same window suppose if you want to perform parallel operations right suppose I have open one application okay and I'll do something and that I will go to another application I have done something again I can go back for example let's
27:15
Speaker A
say even if you don't have a two URLs still you can use this commands Okay even if you don't have a two URLs still you can use this commands I'll give you a simple example let's say you already
27:27
Speaker A
open this applic application let's say this is the application I open this a single URL I'm not using any other application single URL okay in this I already I I click on something let's say I click on some Electronics so it is navigated to another page now again if you go back to the
27:44
Speaker A
previous page how we can go back simply navigate to back so that will go to previous page so with single URL also you can apply all these methods single URL suppose I click on this MacBook now I are entering another page now I want to go back so then you can use backp and again I want to
28:05
Speaker A
move forward MacBook page then you can do forward okay so with single application also you can work with the navigational methods so both the URLs are not mandatory one single URL is also fine okay so I hope everybody is clear on this now move on to the next one get window handle get window
28:27
Speaker A
handles we already know get window handle command will return ID of the single browser window get window handles Returns the IDS of multiple browser windows right but where we will use this actually what is the use of this ID where we will use that ID or where we will use multiple IDs so
28:46
Speaker A
let us see with an example and also we will learn how to switch between multiple browser windows okay so by using that ID we can switch to One browser window to another browser window so I already told you one scenario in the last classes let us say I have two browser windows
29:05
Speaker A
let's say this is one browser window and I have another browser window when I click on something it is open another browser window two open browser windows are there but driver is still focus on the first browser window but I want to perform some activity on the second browser window we cannot
29:22
Speaker A
directly go there we need to switch actually we have to use something called switching commands so by using switching commands we can go to the next page and then we can perform the operation again if you want to go back to the previous page again we have to switch the
29:36
Speaker A
previous page then we can perform the actions so the W get window handle get window handles both will return IDs right and these IDs we have to use along with the switching commands to switch between multiple pages so why we need to switch because our driver is focused only on
29:56
Speaker A
the One browser window at a time it cannot focus on multiple browser windows okay so let's see the use case how exactly we use in our automation get window handles get window handle so let's create another example I'll show you multiple examples and we'll see the use case handle browser windows
30:32
Speaker A
okay so I'm writing some piece of code here shift 2 I put some implicit weight also as soon as I launch my driver this is called implicit make habit of keeping weight statements from now onwards whatever new script you
30:56
Speaker A
are creating just try to put in implicit weight and this is the URL of application I'm opening and then maximizing this window right so if you look at this application right so this is a URL in this I want to click on this
31:13
Speaker A
link let me open fresh page okay so as soon as you launch my web page I try to click on this link link when I click on this link it will open another browser window so now currently I have a two browser
31:34
Speaker A
windows so this is my parent window this is my child window right so let us try to open another browser window and capture the IDS of both the browser window by using those IDs we can switch to One browser window to another browser window okay so for that what I've
31:53
Speaker A
done is I just created the driver and then maximize the page now I want to click on the link inspect this element so this is okay so this is a link here I'm saying driver dot find element by dot X paath you can also use
32:26
Speaker A
Link text no problem and I'm just performing click action so once you perform click action then it will open another browser window so once it is open another browser window now I want to capture IDs of both browser windows parent and child we can let's call it as a parent this is call is a
32:44
Speaker A
child so I want to capture the IDS of both the browser windows so how we can do it driver dot get window handles so this will return IDs in the set format set collection so I'm storing them in
33:01
Speaker A
a variable I say window IDs window IDs and what is the type of this variable it should be set of string so we already seen in the last class so we captured multiple browser windows IDs and storing them in a variable now this variable contains a parent window ID and also child window
33:25
Speaker A
ID both are having so now what I should do is by taking the window ID I can switch to the particular browser window okay so for example I want to capture the ID of the parent window I want to capture the ID of the child window but both are available in the set collection how can
33:44
Speaker A
we get it individually individual IDs from the set collection how can we extract individual IDs parent window ID child window ID currently which is having both two right one one option is there looping statement by using Loop looping statement you can iterate it the other option is what we
34:06
Speaker A
can convert this into list then we can extract right so let's say the first approach approach one because if you have just two IDs un necessarily Loop is wasted right so if you have a more number of IDs let's say five 10 IDs then Loop will be preferred
34:23
Speaker A
most of the times but if you have just two IDs better to convert into list collection say approach one what I'll do is I will convert this set collection into list collection we already seen this concept in Java sessions we can convert set into list right array
34:40
Speaker A
list how we can convert we can create an array list variable or a list variable so list of a string Okay window list window list equal to new array list new array list of window IDs all right now import this array list also import this list so what the statement
35:14
Speaker A
will do this statement will convert the set collection into list collection converted so once you converted into the list collection can we extract IDs individually yes how can we EXT ract window list dot get is a method get so when I say get of zero which ID it will return parent
35:38
Speaker A
browser ID the first ID so that I'm going to store in a variable so I can create one variable called string parent ID similarly how to extract the second window list dot get off one so this will return sh ID okay now I know parent ID and Child ID so what we have done in first we converted the
36:05
Speaker A
set collection into list collection then we have extracted a parent ID Child ID in two different variables now I have a parent ID I also have a Child ID okay now by using these IDs I can switch to One browser window to another browser window suppose as soon as you launch your web page y
36:27
Speaker A
set should be connected to list can anyone answer this question why we have converted set into list collection what is the necessity can anyone answer this question we already covered this in the last sessions why you have converted set into list collection yes so if you have in set collection
36:48
Speaker A
we cannot extract individual items because we don't have a get method in set collection okay if you have a multiple items in this window IDs if you want EXT one by one individually I cannot extract or I cannot access it because set collection don't have a get method to access
37:07
Speaker A
individual items so that is a reason we converted the set into list collection so that in the list collection we can extract individual items by using get method I can extract zero item and first item and so on but in the set collection it will not follow indexing concept first of all so that's
37:26
Speaker A
the reason we don't have a get method we cannot extract a specific item from the set collection because it will not follow index so that is a reason we converted the set collection into list collection type and then we have extracted parent ID Child ID differently okay now what is the use
37:48
Speaker A
of these IDs Now by using this parent ID Child ID I can easily switch to one page to another page so as as soon as you launch this particular page our driver is focused on the first browser window
38:05
Speaker A
okay so as soon as we launch the application our driver is focused on the parent window even if you open the child window still our driver is focused on the parent window our driver is focused on the parent window but how we will know that let us see I'm trying to print driver dot uh
38:28
Speaker A
uh you can say title I'm getting what driver. title get title I'm using see I already open this application then I click on that link that is open another browser window which is child window okay now I'm getting the title so if you're getting the title if it is written the parent return parent
38:49
Speaker A
window title or child window title which one it will return forget about these things okay just I open this application and then click on the link it open another link right so even though if it is open another browser window when I getting the title it will return the parent browser window
39:07
Speaker A
title why because driver is still focused on the parent window only right so if you execute it it will still return return the parent window title it will not return return the title of the child window why because our driver is still focused on the parent window see it is open child window
39:27
Speaker A
now I have a parent window child window but if you look at the title it is still returning the parent window title okay so now I want to switch to the child window I want to switch to the child
39:40
Speaker A
window then I want to extract the title of the child window then how we can do this so here I will try to switch to sh window how to switch driver dot there is a method called switch to
39:58
Speaker A
driver. switch to dot driver. switch to. window in this we have to pass ID whichever browser window you want to switch you need to pass ID of that particular browser window now I'm passing the Child ID here okay after switching then I'm executing this command driver. get title
40:19
Speaker A
now observe so the switching command will able to switch to child browser window and after that it will get the title from the child browser window now this is my parent browser window right now it is open another child browser window parent and child now if I look at the title
40:41
Speaker A
you will get the title of the second browser window previously we got only orange HRM that's the parent browser window title now we got the title from the child browser window okay now we switch to the child browser and we got the title of the child browser window again I want to go
40:58
Speaker A
back to my parent window again I want to switch to the parent window then how can we do it switch to parent window again I want to switch back to the parent window simply what you can do driver
41:16
Speaker A
dot switch to dot window and here the window command will take the browser ID so here we need to pass parent ID okay now again if you capture the title this will give the title of the parent browser window so by using switching command we can switch to One browser window to another
41:41
Speaker A
browser window here the navigation commands won't work okay because navigational command is only for navigating between the multiple Pages forward and backward but it is not for switching our drivers still switch on the parent browser okay so if I look at here now you got the title
42:02
Speaker A
for the par child browser window and this will give the parent browser this this is the title of the parent browser window okay so what is the use of capturing window IDs so now you guys are understood why we need to capture IDs of browser windows this is the use case if you
42:23
Speaker A
capture the IDS of browser windows we can easily switch from one browser window to another browser window otherwise we cannot perform any other operations suppose you open this parent window and you navigate to the CH window now I want to do some validations here I want to click on some
42:41
Speaker A
button or I want to uh find something on this page or I want to do some automation related validations here so how it is possible directly it is not possible we need to switch to the browser window then it is possible so like that we can use a switching command so what is the command
42:58
Speaker A
which we have used is driver dot switch to driver object dot switch to dot window and here we have to pass window ID so this is the overall command so get window handle get window handles whatever IDs we are getting by using those IDs we can switch to multiple browser windows that is a
43:27
Speaker A
use case okay this is one approach let's say I have only two browser windows parent and child so we can convert that into list collection we have extracted parent ID Child ID and then whichever browser window you want to switch you can easily switch it but sometimes if we have a more number
43:45
Speaker A
of browser windows let's say I have a five 10 browser windows are already open then how we can switch so instead of getting all IDs into multiple variables I I can simply use a looping statement okay let me command this this is an approach one it is this approach is recommended only if you
44:05
Speaker A
have a two browser or three browser windows but if you have many number of browser windows then it is not recommended to convert into uh array list okay we need to use a looping statement approach two so we already collected window IDs both the window IDs right let's write a looping statement now
44:28
Speaker A
for by using looping statement we can do it right window ID is I'm capturing into a variable called string and this is enhanced for Loop string uh vard window ID I'm taking one variable so from window IDs I'm getting each and every window ID in this variable so now by
44:49
Speaker A
using this variable window ID I can switch to whichever browser window I want so let us see by using this window ID what I can do is I can switch say driver dot switch to dot window and you can pass window ID so this is got switched but on which browser window it is got switched
45:12
Speaker A
do we know that simply we're getting IDs one by one once you're getting the ID right and we are using that ID in this window but do we know exactly which browser window it is Switched by using the statement we don't know right but in the previously we have only two browser windows
45:30
Speaker A
parent and child we have considered but in this context there are multiple window IDs I got one ID and I switched to it but how we will know whether which browser window it is got switched we don't know that right so what we need to do is we use something called dot
45:47
Speaker A
get title we can use following command called get title so whatever browser window it is switched from that browser window we are extracting the title we are extracting the title that I'm going to store in a title so by seeing the title by seeing the title we will know exactly
46:07
Speaker A
which browser window it is got switched right so now what I will do is I'll put one condition if this title dot equals okay suppose if this title is orange HRM okay what is the type of browser window is the parent or child okay we don't know whether it's parent or child simply I can print
46:30
Speaker A
URL so driver. get current URL like this okay so we are switching to the browser window and getting the title and if the title equal to this one we are just printing the URL okay now just observe how it is going to switch now it is switching to all browser windows one by one
47:00
Speaker A
okay now I can just observe right what I'm saying here is I just captured the title if the title is equals to Orange HRM then only it is getting the title orange HRM is what it is a parent browser
47:16
Speaker A
window title so we are getting the tit URL for the parent browser window okay suppose if you put something else here or else what you can do is I can just remove the if condition okay so I can say if condition I removed so I'm getting the title okay and uh uh this is optional actually
47:39
Speaker A
so title is not needed it is already switched I'm not using the title as part of validation if you want to validate you can just use this can say if if I don't use if condition it will print URL for parent and child both the windows let's say if title dot equals right so here I'll
48:02
Speaker A
say so if you inspect this page you can see the title in the header tag this is a title the title equal orange HRM and this is the driver. get current URL I'm getting if condition else basically we can just do validation based on the title if the title is equal to Orange HRM then
48:36
Speaker A
do some validation here you can put any other validations here some validation on the parent window okay so based on the title we are deciding else is not required suppose you already have multiple browser windows I'm capturing the title and suppose if you want to perform any operation
48:56
Speaker A
on the parent browser window window then title equals to parent window then do the operations on parent window suppose if you want to do operations on the child window then you can change the title here if the title is equal to the title of child window then do the operations on the
49:11
Speaker A
child window so it will capture the titles of all browser windows one by one in every iteration but here what we are doing based on the title we are checking on which browser window and based on the title we are performing the action so basically what exactly we are doing is we are applied some
49:31
Speaker A
logic because we don't know exactly whether it's parent window or child window right let us say I have 1 2 3 let's say this is X and this is y this is z let's say three browser windows currently our
49:44
Speaker A
driver is here and we got IDs of x y and Jed now I want to perform some action on the third browser window here okay so in the in the F Loop what exactly we are doing we are capturing the titles
50:01
Speaker A
of first we are switching into the browser window first let's take a first window ID getting the title some X now second time switch to the second y getting the title called y next round getting the title called Jed so in the if condition what exactly we are doing suppose if we want to perform
50:19
Speaker A
some activity on Jed so then if condition if the title equal to Jed okay then perform the action so rest of the two will be ignored because the title we are matching with the third web page third browser window so that's the reason based on the condition we can filter so on whichever
50:39
Speaker A
browser window we want to perform operations we can specify the title of that particular browser window we can specify the title of that particular browser window suppose if you want to perform action on the child browser window and here you can specify the title of the child
50:55
Speaker A
browser window like that we can switch okay so you understood this problem everyone so when I working with the looping statement we can also perform actions on other browser windows but we need to filter let's say you have 10 browser windows I want to do some action specific browser
51:15
Speaker A
window then what we need to switch to it and get the title and if the title is so and so and then do the action if it is not matching then it will get another window ID another title again it will
51:27
Speaker A
match even if it is not matching then it will get another title so it will navigate through all browser windows wherever it is matching there it will start doing the operations so this is the second approach if you have a multiple browser windows then you can go with this approach if
51:44
Speaker A
I have only two browser windows then go with the convert conver conversion approach like convert to set to the erray list and by using get method you can extract okay so these are the two methods are there so is it clear so far
52:08
Speaker A
everyone yes if you want to do you can do also through iterator but in iterator it will just give only IDs one by one it will iterate you can try that okay so yeah by using iterator also we
52:23
Speaker A
can do this you guys can try that okay so now I will based on this example I will ask you one thing you should able to answer this in the last class I told you one scenario we will try
52:38
Speaker A
to see that scenario once again we have learned browser commands right close and let's say I have multiple browser windows like this this is one 2 3 four and five now if I use close command what happens wherever the driver is uh focused only that particular browser window will be closed
53:02
Speaker A
single browser window will be closed single browser window will be closed when I use quit command all browser windows will be closed but now my requirement is I want to close specific browser window let's say I want to close only three or I want to close only five or I want
53:22
Speaker A
to close two and four or I want to close three and five based upon our choice based upon our choice I want to close specific browser window okay so then how we can achieve this so this is a very very important use case uh during interview people will ask you this yes same
53:46
Speaker A
logic we need to apply so whatever logic here we have implemented same thing let me show you this creating new class closing specific browser window taking main method and say finish okay so now I just copy the same
54:14
Speaker A
code okay this is the code okay so I launch my application maximized it click on the link so this will open another browser window and then I captured both the window IDs all right now I have a two browser windows as soon as you have done this two browser windows are open now I want
54:43
Speaker A
to close whichever browser window I want to I want to close it okay simply what you can do by using looping statement we can do it so take this window IDs into a variable let's say win ID it is a string variable why it is string because all window IDs are in the string format
55:05
Speaker A
okay so now if you try to write like this driver. switch to dot window window ID and Dot get a title okay I'm storing the title in a variable called title then I'm printing the title here so what
55:28
Speaker A
this will do actually so it will keep keep reading each and every ID then switch to that particular browser window and then get the title right if I have 10 browser windows it will print 10 browser window IDs and titles not IDs because we already captured the IDS we are reading each and every
55:46
Speaker A
ID into this variable and by using this ID we are switching into the particular browser window and getting the title so you can have any number of browser windows so this will capture the titles of all the browser windows and it will print right so let me show you and by using the title we put
56:06
Speaker A
one condition whichever browser window you want to close you will put one condition now we can see there are two types are open here first one and second one two browser windows are open and if you look at here you got a two titles okay now my requirement is I want to close specific
56:23
Speaker A
browser window I want to close my parent window for example or currently uh okay parent window I want to close then what I can do just put one if condition if title dot equals this orange HRM Okay then if this condition is match we have to close that browser windows so
56:49
Speaker A
I can say driver Dot close driver. close so this will close based on the condition we are closing specific browser window so now the child is still alive so we don't need any break actually yeah if you want to put break no problem you can put but if you have
57:16
Speaker A
to close multiple browser windows then it will be problem if you put break I will tell you so you can see the child browser window still open but parent browser window is already closed okay suppose if you want to close CH browser window then
57:30
Speaker A
I can put this title now this time it will close only child browser window so now you can see parent and child child browser window should close yes child window is got closed and parent window still open So based on the condition we can close it so if you cling
57:56
Speaker A
only one browser window then you can put a break command no problem in this because I don't need to get any other window IDs because as soon as you get a browser window matched just immediately close it and exit from the F Loop no need to get other IDs but if you want
58:13
Speaker A
to close multiple browser windows let's say I have five browser windows I want to close only one two three okay then you should not put break command and here you can put multiple conditions title do equals this one or or R title do equals whichever browser windows you want
58:31
Speaker A
to close you can specify the titles of those browser window or you can say some other title right so now this will close two browser windows which are matching with this title and also this one because we put R operator R means what either this one or this one so whatever browser windows
58:52
Speaker A
are having these two titles only those browser windows will be be closed or this one or this one here we cannot use break okay because if we put break as soon as the first browser window is got matched it will close it immediately it will break the F Loop but the second one it will not
59:09
Speaker A
consider so that is the reason here break is not applied okay so this is how we can close specific browser window based on the condition okay so based on the condition we can close specific browser window whichever browser windows you open doesn't matter you can close
59:30
Speaker A
specific browser window by putting the condition so this is how get window handles and get window handles most important topic just do one round of practice and so how do we know the title of it yeah before that you know the title actually suppose if you want to close whichever browser
59:52
Speaker A
window you want you can capture the title just by right clicking inspecting the page and the header part you can see the title actually in the header you can see the title tag in that you can find the title so because whenever you want to close your browser window you have a plan
60:08
Speaker A
right so you need to capture the title from this browser window and put the title in the condition so like that you can close whichever browser window you want because the title must be there otherwise we cannot know which browser window you want to close because if you want to
60:23
Speaker A
close something you have to know something about it right you have five browser windows fifth one you want to close then you have to know the title of the fifth one or if you want to close one two three first three browser windows then you have to know the titles of 1 2 3
60:37
Speaker A
like that and you can get the titles from the Dom itself from the HTML of the page you can get the titles so by using window ID you don't know right which browser window it is that's the reason we are getting the title here so you're passing window ID that's fine it is switch to the
60:56
Speaker A
particular browser window but you do you know exactly by using window ID do you know which browser window it is belongs to no right suppose I have captured window IDs here let us say here I capture both the window IDs so by seeing the window ID can we know which browser window that
61:17
Speaker A
ID is belongs to do we know exactly no by window ID we cannot know that and moreover the window ID is dynamic every time it will generate the new window idid so by using window ID we don't know which browser window it is pointing so that is the reason we are switching to the particular
61:36
Speaker A
browser window and then getting the title so by using Title by knowing title we will know which browser window it is pointing right so that's the reason we are getting the title here if we don't have a title we won't know we will not know which browser window it is pointing so
61:52
Speaker A
that's the reason additionally we added get title after switching to the browser window capturing the title and based on the title we are closing the particular web page so this is how we need to do it okay so this is how we need to switch between multiple browser windows there
62:12
Speaker A
are three kinds of switching commands are there we can switch to multiple browser windows we can switch to alerts we can switch to frames so alerts and Frames we will discuss in the next uh sessions so how to find the title from the URL so title from the URL is not directly possible either
62:33
Speaker A
you can capture the URL of the web page or you can capture the title of the web page suppose here we are calling get title right instead of get title you can capture the URL also okay suppose I'm
62:46
Speaker A
trying to capture the URL you can put condition on whichever way you want so here you can call get current URL so this will return the URL of the page okay in that case you can put the condition
63:00
Speaker A
as on URL if the URL is so and so then close it so this is possible but from the URL we cannot get the title until unless you launch this URL on the browser if you launch this URL in the browser
63:13
Speaker A
then you can get the title but directly from the URL you cannot get the title because both are two different commands we can access from the driver okay so this condition should be based on the URL or it can be based on the title so whichever way you want you can use it but from the URL
63:35
Speaker A
you cannot get directly get the title until unless you launch the page suppose here you launch your url here so then by using driver. get current URL or get title you can get the title yes title is also visible on the window tab yes can see here this is the title on the top tab
63:58
Speaker A
the name is showing here you can see that can you see the tool tip Human Resources management software orange HRM can you see that that's called title actually and here orange HRM that is a title so on the tab itself the title are shown suppose if you're not able to see here you can inspect
64:17
Speaker A
the page and go to the header head and here you can see the title tag directly you can use it okay so that's all for today's session I'll stop here I'm still not feeling well I'm just stop here for today's session and tomorrow we will continue and I will give
64:38
Speaker A
you an assignment on today's session you guys can try this okay so that assignment is yeah so just go to this page so what you need to do is in this page you can see something called uh a search box so here you can see the tabs right so here you need to provide some search string it
65:09
Speaker A
can be anything let's say I'm providing something called selenium okay then click on the search button then some links are displayed so what you need to do is First Step provide some string and search for it then count a number of links so as soon as you have search some links are displayed
65:29
Speaker A
right you need to capture these links and find how many links are uh displayed here so how many links are there 1 2 3 4 5 six totally six links are including this more six links are displayed so you need to count how many links are there the next thing is Click on each link using F Loop so
65:51
Speaker A
you need to capture each and every link from the web page from this link need to read read each and every link test capture each and every link testt and perform click action on it if I click on this
66:01
Speaker A
observe you need to use a f Loop here as soon as you click on the link it is opening another tabs can you see it it is opening multiple tabs so how many links are displayed here you need to click
66:15
Speaker A
on each and every link that opens multiple tabs that's the next step and after that get window IDs for every browser window and then close specific browser window so capture the IDS of every browser window by using get window handles and then capture the titles and close whichever
66:40
Speaker A
browser window you want to close so I have 1 2 3 4 five browser windows are open and you can close whichever browser window you want based on the title so this is assignment for today you guys can try I will provide the solution also I will upload the solution for this but
66:56
Speaker A
you guys can do first okay and then look at the solution for this so this is again depend on the today's topic fine so try to do this and tomorrow we will continue
Topics:Selenium WebDriverJava Seleniumnavigate.todriver.getbrowser navigationSelenium commandsWebDriver methodsSelenium tutorialnavigate.backnavigate.refresh

Frequently Asked Questions

What are the main navigational commands in Selenium WebDriver?

The main navigational commands are navigate.to, navigate.back, navigate.forward, and navigate.refresh, which allow you to open URLs, move back and forward in browser history, and refresh the page.

What is the difference between driver.get() and driver.navigate.to()?

Both methods open a URL in the browser and perform the same function, but driver.navigate.to() can accept URLs in both string and URL object formats, while driver.get() accepts only string format. Their internal implementations also differ.

Can navigate.to() accept a URL object as a parameter?

Yes, navigate.to() can accept a URL either as a string or as a URL object, providing more flexibility compared to the get() method.

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 →