Hello Everyone,
Today i will be blogging about THE most asked question about selenium/webdriver. That is, switching to popup window using web driver. I heard this question from many people, saw it as a question/explanation in many blogs.
I found it difficult while implementing Selenium in my company also. The situation is gets worse when there is more than 1 popup at a particular time and window names are same -:(. This is bad, isn't it?
Ok let us see the solution now. CODE it DOWN!! Before that create a test scenario. You have a application, you login to the application, Home page shows up. Click on any link, it opens up a popup window,You have to do some operation in that window.
Below is the code to for the above scenario. Let me explain one by one.
In the above line of code, we are fetching the current window handle. This you need to take for the home page in the above test scenario.
Why are we doing this?
Here goes the explanation, this is really required for you to understand. When you do have main window and popup window title same then there is only one thing left which is different i.e. Window Handle. a unique number generated for each and every window. We need to play with that id in the code.
Next line is for your test code to do some operation to open up the pop up window. Your pop up window is open now. Now there are 2 window handles currently present.
Explanation for next line of code is like -: You already have X(sWinHndl) and now you have X & Y (lisOfWinds). Compare if it is not X then switch i.e. switch to Y.
This piece of code will only work if there are 2 windows opened at a particular time. But how do you handle it for more than 2 windows, for each window you open, do you want to keep the window handle?? Think and let me hear from you also. Trick is still in the window handles, see how you can use it effectively.
Please feel free to drop a comment if you found this information is helpful for you. If you have any comment/suggestion/issue with anything mentioned above, then also please share it with us.
Thanks!!
Today i will be blogging about THE most asked question about selenium/webdriver. That is, switching to popup window using web driver. I heard this question from many people, saw it as a question/explanation in many blogs.
I found it difficult while implementing Selenium in my company also. The situation is gets worse when there is more than 1 popup at a particular time and window names are same -:(. This is bad, isn't it?
Ok let us see the solution now. CODE it DOWN!! Before that create a test scenario. You have a application, you login to the application, Home page shows up. Click on any link, it opens up a popup window,You have to do some operation in that window.
Below is the code to for the above scenario. Let me explain one by one.
See the second line of code,
String sWinHndl=wd.getWindowHandle();
In the above line of code, we are fetching the current window handle. This you need to take for the home page in the above test scenario.
Why are we doing this?
Here goes the explanation, this is really required for you to understand. When you do have main window and popup window title same then there is only one thing left which is different i.e. Window Handle. a unique number generated for each and every window. We need to play with that id in the code.
Next line is for your test code to do some operation to open up the pop up window. Your pop up window is open now. Now there are 2 window handles currently present.
Explanation for next line of code is like -: You already have X(sWinHndl) and now you have X & Y (lisOfWinds). Compare if it is not X then switch i.e. switch to Y.
This piece of code will only work if there are 2 windows opened at a particular time. But how do you handle it for more than 2 windows, for each window you open, do you want to keep the window handle?? Think and let me hear from you also. Trick is still in the window handles, see how you can use it effectively.
Please feel free to drop a comment if you found this information is helpful for you. If you have any comment/suggestion/issue with anything mentioned above, then also please share it with us.
Thanks!!
This post is really nice and proven to the query related to pop-up handling in WebDriver.
ReplyDeleteThis post has proven to be a great learning for me to start dealing with pop-ups in webdriver.
Thanks Dwarika!!
ReplyDeleteHi,
DeleteThanks for informative article. Am able to handle two windows at a time using getWindowHandle(). But facing problem in handling more than 2 windows . It would be helpful if u provide me with the solution.
Thanks in Advance
Hi,
DeleteJust a quick solution, you can keep window handles in array list and manipulate the list as and when you are getting a new window handle.
Thanks
Dibyaranjan
Unable to switch to Alert without closing window popup in webdriver.lost the control of main driver
ReplyDeleteScenario:
Enter a keyword (Position) in a textbox
press Tab or click on the next element
A window popup appears
a list of position matching the criteria will be listed
click the required record
Popup window closes automatically
Alert will be displayed
have click on ok button of alert.
switch to main window and check the position details
Question :- i found many solutions in the site but all are about performing actions on window popup and close the popup then switch to alert or main browser
but my application closes the popup window automatically after selection,hence i cannot use the driver.close() or driver.switchto.defaultContent().
If i dont close the driver (for popup) im not able to identify the next alert displayed after popup.
if i switch directly to mainwindhandle, the alert dismiss is happening instead of accept.
Please anyone who know how to switch from window popup to alert without popup close help is needed...
Thanks ....
Thank u very much..........its really nice....sweet and short...
ReplyDeleteI am new to Selenium. ... I have iframe in that there are two frames.... I switched to one frame and did the activity, wants to move to other frame, but it is not happening, not sure whats the issue is... any help will be appreciated
ReplyDeleteShould switch to default content before switching to next frame
DeleteHi,
ReplyDeleteThanks for your informative article. Am able to handle two windows using getWindowHandle() at a time. But facing trouble in handling more than 2 windows. It would be helpful if you provide me with the solution ..
Thanks in advance
Hi,
ReplyDeleteThanks for informative article. Am able to handle two windows at a time using getWindowHandle(). But facing problem in handling more than 2 windows . It would be helpful if u provide me with the solution.
Thanks in Advance
Hi,
ReplyDeleteThanks for informative article. Am able to handle two windows at a time using getWindowHandle(). But facing problem in handling more than 2 windows . It would be helpful if u provide me with the solution.
Thanks in Advance
Hi,
ReplyDeleteThanks for informative article. Am able to handle two windows at a time using getWindowHandle(). But facing problem in handling more than 2 windows . It would be helpful if u provide me with the solution.
Thanks in Advance
Hi,
ReplyDeleteThanks for informative article. Am able to handle two windows at a time using getWindowHandle(). But facing problem in handling more than 2 windows . It would be helpful if u provide me with the solution.
Thanks in Advance
Hi,
ReplyDeleteThanks for informative article. Am able to handle two windows at a time using getWindowHandle(). But facing problem in handling more than 2 windows . It would be helpful if u provide me with the solution.
Thanks in Advance