Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
4.2k views
by Contributing Tester (12 points)
This question was asked to me in an interview. I am not able to move to second window and close it. Please help me.
by Expert (572 points)
can you please share your code. So we know what have you already tried? Also, provide any error stack trace.

2 Answers

0 like 0 dislike
by Expert (572 points)
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){

driver.switchTo().window(windowHandle);

}

public void switch_to_window(String currentWindowHandle, String title){

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

while(String handle : handles){

if(!handle.equals(currentWindowHandle)){

driver.switchTo().window(handle);

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

}

}

}
0 like 0 dislike
by The go-to Tester (161 points)
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


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...