This question was asked to me in an interview. I am not able to move to second window and close it. Please help me.
commented by (552 points)
can you please share your code. So we know what have you already tried? Also, provide any error stack trace.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

2 Answers

answered by (552 points)
0 votes
I can help you with generic answer here.

1.) Using driver.getWindowHandles() and store all Window Handles to List<String> variable.

2.) Store current window handle using driver.getCurrentWindowHandle(). So, you are sure you do not switch to the current window during iterating.

3.) Store IInd window title into any string variable say window2title.

4.) Store IIIrd window title to String variable say window3title.

Now you can loop through as given below.

public void sample_psudo_swith_code(){

String currentWindowHandle = driver.getCurrentWindowHandle();

String window2title = "your  title here";

String window3title = "your title here";

switch_to_window(currentWindowHandle,window3title );

//... do your work here with window 3

switch_to_window(currentWindowHandle,window2title );

driver.close(); //switching closing IInd window.

switch_to_window(currentWindowHandle );


public void switch_to_window(String windowHandle){



public void switch_to_window(String currentWindowHandle, String title){

List<String> handles = driver.getWindowHandles();

while(String handle : handles){



if(driver.getTitle().equals(title)) return;



answered by (161 points)
0 votes
I would suggest you to first understand the question and then logic and then you will be able to code at your self

1. Lets open the 3 window

2. Capture the Window ID of these 3 windows and save in the List

3. navigate to 3rd window and copy the text

4. navigate to 2nd windows through the saved windowID