How to Handle Multiple Windows or Tabs in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a hydrogen this is bouquet Shivani from learn - automation calm today in this video we'll see how to handle multiple windows in selenium or I will see how to handle multiple stacks or I will see how to handle 10s pop-ups in selenium webdriver so let's pass the session so in this video we'll start with what exactly I mean by multiple windows or tabs what is form of handling how to handle advertisement pop-ups which we generally get in some dog food do side then we will also see how to handle multiple windows using selenium webdriver in different approaches so you will love this session because here we will cover multiple ways to cover multiple windows or tabs so we'll also get live ready for this so because this scenario will be taper off on kind of application so better we will click when lab ready for this and we can start using it in our framework so same video I have laundered a floated long back around Julius back but it has some sound she like one dome is very less so I'm just creating same video but with additional information and some new tips and tricks I have already uploaded the course or code but here I have our own shooting iterators but in this video we'll see using um indexes and using an m4 new bedford so first of all let me show you what exactly I mean by multiple taps window so I have clicked it one small demo application here you can see three different links with the same name then click on this it opens in a new tab I click another link over again in a new tab and if I open again third window it opens the outer so whether it's a new window or tab selenium webdriver will consider this as a separate window okay each tab or each window will have a separate session ID using that we can eat this too so doesn't matter whether you're opening a tab because same application or same application time application is open in Firefox you will see the different behaviors right click here it's opening in a tab tab and again use that but when you run through selenium it might open in a new window depends on the browser settings so it doesn't matter code will remain same approach will remain same okay and you can also see when more links I see click this link to start session in the same window it will simply go to the Facebook of the team tap on window so this approach we will see how we can easily switch now if you open up Chrome or any other web portal job portal before you can see it's blocking some of the pop-up but if I say enough pop-up and refresh this side let me open again just because and if I open Orkut or come again you can see the separate pop-ups are coming right and each of is nothing but a separate window I will get its min mail button max mails button so it also separates window so I need to click on this here once you can see I have separate window it again for some company and we are here we also need to close so it's a separate window only to manual take there two times moon pop-up or some other purpose it's nothing but a separate window so we need to switch to them and tendon explode so if you open the same knock it on come here click update tab and you will see to start the same pop up in a new tab same thing we'll just do it and we'll close it but let's focus on this scenario first then we will keep this locket out from scenario so first of all selenium has provided as two different methods to handle multiple windows so if I show you the official documentation this is off here documentation on tips of dot IO so here we have one method concat window handle and if you see the description they clear here today April written we unique handle or string to this window which will uniquely identifies it within that private instance and we can use this variable or resistance to switch to the parent window later date they have very interesting method forget window handle what it does is to writ any number of window will be opened by the webdriver session so if you have my windows open it will return the counter side if you see window opens it will return account is free and if you see the return type of this is set off strength so if you have not gone through my set and yes we do ever recommend you to go through it first because then only we'll get the clear picture of how the space works and what is the difference between less efficient x and y selenium - has given only set not little okay so just go to a list in that video you will get to know why the head table set I will explain through code so we'll click a new package here and this packet will married you handle window and I will create a separate class here called handle window one I will to take public static void main if you are using testing GJ NIT you can take I will start the session directly with webdriver driver equal to new chrome driver and since I'm using Mac and store all the drivers at specific location so if I go to users folder pin here I have F on the Chrome browser gecko drivers so by by default my test will take all the drivers from this location and it will start the session so I do not have to set everything and application URL I will give the demo URL which after this I will I will click on the fourth link so I have written a small X spot which will click on the first link you cannot share how to write the X part you can visit my expert video where we have discussed how to write x5 so as of now I have written the champ now before picking I need the parent window ID as well so that I can switch whenever I have to switch to the finance window so we have method name is at window handle it will return you the parent window handle name and if you want to cross verify you can print here like parent window ID is parent it'll print okay so when we click definitely it will start a new tab or window so if I need to get the number of windows ID I can again call one method called driver got it window handle as you can see the return type is set of the strings so it will return use set of strings and till it returns it so it will not have any duplicate value so we can easily switch I will give one name as all windows and it is asking what is sex I will import it from Java dot util ok now we got the parent window ID now with all the handles now we just have to do a small logic here we need to put small logic enter the number of windows and check if it is not equal to parent then switch to the chain window in order to get all the values I can write small for loop in s for loop it crater as of now I can write a very paste for loop I will check first of all how many windows are there and I will print I will say total window is count I can run a for loop now and check if which is open after clicking on this if it is not equal to parent then system they will write a small for loop it will start with 0 and it will go till the number of windows we have an iceless to inside for loop what I will do anything it to set so we cannot use get here so better we can use in end form because in else for loop it will automatically create the value one by one so I will use this drink child and all the windows keep returning values it will have find one and chain one so I will write a small logic here if this is not operator if parent is not equal to j then definitely sister it so will compare with this if the parent ID is not equal to check then switch which to driver tour twist 2 dot window so switch to dot which is the command to switch to the window after sitting we can do any operation so if I open google.com every gift this search box if I inspect this I will get one property called name and name here is QQ for 12 so we'll just try Trevor dot find element by move Q and thankee selenium-webdriver and after 2-3 seconds we can close this window so I will put travel to sleep of 3 seconds and filter exception and I woke you I cannot use quit here because if I use driver code it will close my parent window as well so once I'm done with mellitus activity I can directly the driver dot switched to dot window paraffin and here also I can do any other activity after coming back to the parent let's say I need to print the title so after coming to this parent window I will stay here parent window title I will turn other scenario cessful this is just for one window and I will run in debug mode so I will start the session with debug mode I have kept one debug point at line number 14 it says when I run the first light or statement it will start the new session you can see the session started and and I hit the first command to start the application is done now now we will run this commands to get window handle returning parent window ID which is some big empty string enter 34 right now as usual I will click on the first link it will open in a new tab or window it doesn't matter up to the processing now able to get window handles so what it will do it will give me this window ID in this window 84 and I will show you if you put mouse over you will see we have to value say right now so 1 will be or Paris Pinto 80 which is this and this is chal window NT correct now what we'll do we'll first of all take the size which is a 2 now we'll run a small in a for loop what it will do it will take the first value it will store in gel variable so there is no child variable have same ID which we have for the parent because it is this both the valued so it gave me the first value now we are comparing if parent is not equal to change it is false because both are same as of sausage will not go inside this into statement again it went to the for loop this time it will take the second value which is actually the second window where you can see it ends with 71 here we are checking if parent is not equal to j EF and now we'll switch to the parent-child window you will see when I run this command automatically control is going to the Google now if I run this command driver dot finder is by name cube you can see right it diverts lenny webdriver after 3 seconds if I say driver dot close it will close only that actor session and again just checking to be have more windows definitely not click will come out of this for loop you'll switch to the parent window which is this and now we will get that is just like very very easy code and it is replicable when you have simply one window or then you want to close the form now let's know spirit form or any other application where you have multiple purpose this time I will just go to na 3 dot forms it's just a joke total and you can see it's coming as a separate to pop ox which is nothing but windows and so what code change is making is very small for change since it's an advertisement pop-up so I don't have to do any activity on this what I will do after switching to the advertising pop up I will simply close them so this is the code when you have to close are the advertisement pop-ups and want to see whether they have switch to the chat window or not you can print the titles as the tail window I tilt a driver Todd gets title it's if I again right-click and this time every book form may be directly from the line purchasing so I will right click and let me close the previous instance epoch s java application again new session started with blank as a flaw and when I hit the query it is starting my application with two pop-ups keep them here as a snow man we just to show you when I say parent like what the parent ID which is but in interesting interest e CD but when I say giving the all window handles it will return me three different window and as n if you see here this is ECG which is the parent wind away this is childe Wynd oid like pop of one and four forces right now I will take the size definitely cows will be three I will prove that this event for loop again I will check it will take the first string I am checking whether it's equal to parent or not if I see first iteration it gave me the exact the same parents and why these definite teeth will not to entice this if conditions when I run this code again not given seven second string ID with different it means it's a child window it means it's a advertising pop up it will go inside this pushed to that window print the title wait for three seconds and you will see it will close it sopra advertisement popper now again it is taking one more string from that loop this time ID is different so it will against which the child window and you can see now it's for Amazon it will print the title and you can see Titus printed Amazon wait for 3 seconds and closes and that's all again it is taken to be ham or window definitely not it will come back to the parent window and you can see this highlighted and you will print the panel finger like the same code you can use for handling windows like multiple windows or / handling and present proper modeling and so on but we also come across multiple situations where we have multiple tabs open and now we need to switch to the specific tabs from the operation and come back to the parent page again just go back to this second or fourth tab and do some operations right so in that case what we can do and this is interesting as well if you have been my text video I covered the same thing how we can convert set into lists so same thing will do now once I get this set you don't need this lambda code now what you can do you can create your own error list first of all make sure the error list which is sleep should be type of a string because the set which we have of a string type only here I will save multiple tabs or any name you can give and just pop this all windows here what it will do it will convert this set into an array list so if I pass all windows here if we convert into analysts no I will have this tabs option so taps is nothing but the object of access so if I paste abs dot get if I say 0 it will switch to the parent window if I say tabs dot get one just to the second with a bar window and if I see a return meter window handle of Howard window or tab let's imagine how is great will return a string get one also will return the string so if I say driver dot switch to dot window F dot get of to it was just to the third window and I will take close then I will say to the second so now if I have to go back to the parent window which is man doctor.com 0.62 dot window tabs dot get of zero and I cancel the title I turn of parent window is triangle dot get tight it's very easy approach you don't have to write the conditions iterator now it's up to you which tab you want to go and for opticians but in this case you should know in which order they're coming so if you want to see again I can debug this and show you right let's keep against job position animate the previous session starts affectionately syllable rashes our application role session started form with two different windows get window handle and you can see when I say tap I got all the list all the set in terms of list so when I say switch to the third one close you can see it close when I say f1 it is second close zero correct and I can stop it it's very easy when you have inverted into list you can do the same thing here if you go to my example I have treated the same example so when you click here so you can take this or your assignment and click on the third you get three different window handle and use the same code and vote Emma wounded you want to switch to the port window which is Google or Yahoo you need to see in which order your open okay so if you click on this link first Google will be the first if you click on Facebook then it will be second and mask on will be young this with a different tab to your activity close this window switch back to any other window clear activity close this window same thing you can do for Yahoo let's try this it's very easy for you to convert into this and Justin you remember this is the finally command driver or switch to dot window this will help you to switch to the different tabs on okay this is the example and that's all I have for this video if you like this video kindly share with your friends like share subscribe to my channel click query rate to twistable human java let me know thank you so much have a nice day
Info
Channel: Mukesh otwani
Views: 140,191
Rating: undefined out of 5
Keywords: selenium webdriver tutorial, selenium webdriver, mukesh otwani selenium, selenium, test automation, How to Handle Multiple Windows or Tabs in Selenium Webdriver, Handle Pop up in Selenium Webdriver
Id: pZdMsLKAjs4
Channel Id: undefined
Length: 25min 40sec (1540 seconds)
Published: Sat Jul 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.