Do you have questions ? We got the answers!

Ask a question:

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

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

1 Answer

0
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;

}

}

}
answered Sep 30 by SunilBhaskar (430 points)

Top Contributors

  • 4970 pointsstbadmin

    34 questions 454 answers218 comments

  • 430 pointsSunilBhaskar

    40 questions 14 answers26 comments

  • 425 pointsMehedi

    36 questions 3 answers14 comments

  • 354 pointsSoft Tester

    43 questions 0 answers4 comments

  • 342 pointssumitkumarwatts

    0 questions 12 answers0 comments

  • 327 pointsKeeshaGolden

    0 questions 17 answers2 comments

  • 322 pointsVipul Sharma

    44 questions 0 answers24 comments

  • 310 pointsvysh.sharma

    25 questions 0 answers0 comments

  • 267 pointsOnkar_Nimje

    33 questions 15 answers15 comments

  • 258 pointsVanitha

    26 questions 0 answers6 comments

...