Java Selenium WebDriver - correct exception handling techniques when unable to locate window

I'm using Java and Selenium to automate some test cases. This involves loading a single page with search results and iterating over each 100-1000 links on that single page. Setting the test config to only check about 100 results will usually be okay, but anything higher than that and at some point a NoSuchWindowException is thrown(subclass of WebDriverException). This happens when I switch from the the parent handle to the newly opened window handle.

I've written a try-catch statement inside a do while loop to catch the exception and retry the procedure... However, whatever I try, Selenium will not play nice, and my code execution abruptly ends.. :( Here is the code:

boolean completed = false;
            //click the search result
            driver.findElement(By.xpath("my xpath string")).click();
            //switch to the new window
            for(String winHandle: driver.getWindowHandles()){
            //for our test we need to save the source       
            source = driver.getPageSource();
            //close popup window and and switch back to the parent handle
            completed = true;
        }catch(WebDriverException ex){
            System.out.println("something went wrong while switching windows... retrying");

I've experimented with various approaches when an exception is caught. For example, i tried saving the parent url, using driver.quit() followed by trying to restart the driver. However, then Selenium complained about I wasn't able to start the Firefox driver after calling driver.quit()...

Any ideas how to better handle the catch part of my code?


Okay, I've found the solution thanks to the comments left by Tala.

I simply removed the following statements from the catch block:


Now the the catch block is not generating a separate uncaught exception by itself and the loop correctly does it work retrying the previous code...

Need Your Help

installation? whats there behind screens of linux?

c++ c linux installation libraries

I would focus on libraries though it can be a general application installation as well.

android restricting apk install for small screens

android android-manifest apk

How can I restrict my apk to not install on small devices, I know about the support-screens parameter but as I understand correctly , it won't work if user is installing directly from apk.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.