Join 500+ Software Testing professionals. Solve problem and get your problem solved. Register Now!

Write a code to get the text available in 3rd window and move the control to 2nd window and close it.

Open 1 Answers 289 Views
0 1
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.

1 Answer

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 (552 points)

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.